二叉树全网最详细整合

首先了解二叉树的3种遍历

 1.

给你二叉树的根节点 root ,返回它节点值的 前序 遍历。

class Solution {

    public List<Integer> l = new ArrayList<Integer>();
    public List<Integer> preorderTraversal(TreeNode root) {

        if(root == null)
        return  l;

        getList(root);
        return l;
    }

    public void getList(TreeNode root) {

        l.add(root.val);

        if(root.left == null && root.right != null) {
            getList(root.right);
        }else if(root.right == null && root.left!=null) {
            getList(root.left);
        }else if(root.left!=null&&root.right!=null){

            getList(root.left);
            getList(root.right);
        }else
        return;
    }
}

2.

给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {

        List<Integer> list = new ArrayList<Integer>();
        inder(list,root);
        return list;
    }

    public void inder(List<Integer> list,TreeNode root) {

        if(root == null)
        return;

        inder(list,root.left);
        list.add(root.val);
        inder(list,root.right);
    }
}

3.

给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历 

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {

        List<Integer> res = new ArrayList<>();
        getMax(root,res);
        return res;

    }
    public void getMax(TreeNode root,List res) {

        if(root==null)
        return;

        getMax(root.left,res);
        getMax(root.right,res);
        res.add(root.val);
    }
}

4.

给你两棵二叉树的根节点 p 和 q ,编写一个函数来检验这两棵树是否相同。

如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
    if(p==null && q==null){
        return true;
    }

    if(p!=null && q!=null && q.val==p.val  ){
        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }else {
        return false;
    }
}
   
}

 5.

class Solution {
    public boolean isSymmetric(TreeNode root) {

        if(root == null)
            return true;

        return isS(root.left,root.right);
    }

    public boolean isS(TreeNode r1,TreeNode r2) {

        if(r1 == null && r2 == null) {

            return true;
        }

        if(r1 == null || r2 == null || r1.val != r2.val) {

            return false;
        }

        return isS(r1.left,r2.right) && isS(r1.right,r2.left);
    }
}

 6.

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例:
给定二叉树 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

class Solution {
    public int maxDepth(TreeNode root) {

        if(root == null)
            return 0;
        return Math.max(getMax(root.left,1),getMax(root.right,1));
    }
    public int getMax(TreeNode root,int count) {

        if(root == null) {
            return count;
        }

        return Math.max(getMax(root.left,count+1),getMax(root.right,count+1));
    }
}

7.

给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树。

高度平衡 二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。

class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {

        return getTree(nums,0,nums.length - 1);
    }

    public TreeNode getTree(int[] nums,int left,int right) {

        if(left > right)
        return null;

        int mid = (left + right) / 2;

        TreeNode tree = new TreeNode(nums[mid]);
        tree.left = getTree(nums,left,mid - 1);
        tree.right = getTree(nums,mid + 1,right);


    return tree;
    }
}

8.

class Solution {
    public boolean isBalanced(TreeNode root) {

        if(root == null) {
            return true;
        }

        return balanced(root) != -1;
    }

    public int balanced(TreeNode root) {

        if(root == null)
        return 0;

        int leftH,rightH;

        if((leftH = balanced(root.left)) == -1 || (rightH = balanced(root.right)) == -1 || Math.abs(leftH - rightH) > 1)
        return -1;

        return Math.max(leftH,rightH) + 1;
    }
}

 9.

给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明:叶子节点是指没有子节点的节点。

class Solution {
    public int minDepth(TreeNode root) {

        if(root == null)
        return 0;

        if(root.left == null && root.right != null)
        return 1+minDepth(root.right);

        if(root.right == null && root.left != null)
        return 1+minDepth(root.left);

        return Math.min(minDepth(root.right),minDepth(root.left)) + 1;
    }
}

 10.

给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。

叶子节点 是指没有子节点的节点。

class Solution {

    public boolean is = false;
    public boolean hasPathSum(TreeNode root, int targetSum) {

        if(root == null)
        return false;

        dfs(root,root.val,targetSum);
        return is;
    }

    public void dfs(TreeNode root,int sum,int goal) {

        if(root.left == null && root.right == null) {

            if(sum == goal)
            is = true;
            return;
        }
        if(root.left != null)
        dfs(root.left,sum + root.left.val,goal);

        if(root.right != null)
        dfs(root.right,sum + root.right.val,goal);
    }
}

 11.

class Solution {
    public TreeNode invertTree(TreeNode root) {

        if(root == null)
        return null;

        TreeNode r1 = root.right;
        root.right = invertTree(root.left);
        root.left = invertTree(r1);

        return root;
    }
}

 12.

给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。

百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”

例如,给定如下二叉搜索树:  root = [6,2,8,0,4,7,9,null,null,3,5]

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        
        TreeNode r1 = root;
        while(true) {

            if(q.val < r1.val && p.val < r1.val) {

                r1 = r1.left;
            }else if(q.val > r1.val && p.val > r1.val) {

                r1 = r1.right;
            }else
            break;
        }

        return r1;
    }
}

 13.

class Solution {

    public List<String> res = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {

        dfs("",root);
        
        return res;
    }

    public void dfs(String sum,TreeNode root) {

        if(root == null)
        return;

        if(root.left == null && root.right == null) {

            sum += String.valueOf(root.val);
            res.add(sum);
        }

        sum += String.valueOf(root.val)+"->";

        dfs(sum,root.left);
        dfs(sum,root.right);
    }
}

 14.

class Solution {
    public int sumOfLeftLeaves(TreeNode root) {

        if(root == null)
        return 0;

        int res = 0;

        if(root.left != null && root.left.left == null  && root.left.right == null) {

            res += root.left.val;
        }

        return sumOfLeftLeaves(root.left)+sumOfLeftLeaves(root.right)+res;
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海边的彩虹与你

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值