leecode-1305-两棵二叉搜索树中的所有元素
作者:互联网
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
inorder(root1);
inorder(root2);
Collections.sort(list);
return list;
}
// 辅助函数,中序遍历
public void inorder(TreeNode root) {
if (root == null) return;
inorder(root.left);
list.add(root.val);
inorder(root.right);
}
}
标签:1305,TreeNode,val,树中,list,leecode,root,inorder,public 来源: https://blog.csdn.net/hmyqwe/article/details/112254255