总结先放在前面:
二叉搜索树的中序遍历是递增的
解答二叉树时的一些小技巧与注意点:
无
题目实战
1.NO.617. 合并二叉树
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public TreeNode MergeTrees(TreeNode root1, TreeNode root2) {
if(root1==null){
return root2;
}
if(root2==null){
return root1;
}
TreeNode root = new TreeNode(root1.val+root2.val);
root.left=MergeTrees(root1.left,root2.left);
root.right=MergeTrees(root1.right,root2.right);
return root;
}
}
2.NO.700. 二叉搜索树中的搜索
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public TreeNode SearchBST(TreeNode root, int val) {
if(root==null){
return null;
}
if(root.val==val){
return root;
}
return root.val<val?SearchBST(root.right,val)