力扣刷题笔记 二叉树篇02——二叉树的平衡,子树,深度,高度,路径

总结先放在前面:

本篇中练习了如下题目:
判断二叉树是否对称——NO.101. 对称二叉树
判断二叉树的子树——NO.100. 相同的树,NO.572. 另一棵树的子树
计算二叉树的深度和高度——NO.104. 二叉树的最大深度,NO.111.二叉树的最小深度
判断二叉树是否平衡——NO.110. 平衡二叉树
寻找二叉树的所有路径——NO.257. 二叉树的所有路径

解答二叉树时的一些小技巧与注意点:

感受递归的三部曲:
1.确定递归函数的参数和返回值
2.确定终止条件
3.确定单层递归的逻辑

题目实战

1.NO.101. 对称二叉树

在这里插入图片描述

/**
 * 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 bool IsSymmetric(TreeNode root) {
        if(root==null){
            return true;
        }
        return myIsSymmetric(root.left,root.right);
    }

    public bool myIsSymmetric(TreeNode left,TreeNode right){
        if(left==null&&right!=null){    //左空,右非空,返回false
            return false;
        }
        else if(left!=null&&right==null){//左非空,右空,返回false
            return false;
        }
        else if(left==null&&right==null){//左右均空返回true
            return true;
        }
        else if(left.val==right.val){   //左右均非空,比较值,如果相等再递归
            return myIsSymmetric(left.left,right.right)&&myIsSymmetric(left.right,right.left);
        }
        return false;
    }
}

在这里插入图片描述

2.NO.100. 相同的树

在这里插入图片描述

/**
 * 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 bool IsSameTree(TreeNode p, TreeNode q) {
        if(p==null&&q!=null){
            return false;
        }
        else if(p!=null&&q==null){
            return false;
        }
        else if(p==null&&q==null){
            return true;
        }
        else if(p.val==q.val){
            return IsSameTree(p.left,q.left)&&IsSameTree(p.right,q.right);
        }
        return false;
    }
}

在这里插入图片描述

3.NO.572. 另一棵树的子树

在这里插入图片描述

/**
 * 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 bool IsSubtree(TreeNode root, TreeNode subRoot) {
        if(root==null&&subRoot!=null){
            return false;
        }
        if(IsSameTree(root,subRoot)==true){
            return true;
        }
        return IsSubtree(root.left,subRoot)||IsSubtree(root.right,subRoot);
    }

    public bool IsSameTree(TreeNode p, TreeNode q) {
        if(p==null&&q!=null){
            return false;
        }
        else if(p!=null&&q==null){
            return false;
        }
        else if(p==null&&q==null){
            return true;
        }
        else if(p.val==q.val){
            return IsSameTree(p.left,q.left)&&IsSameTree(p.right,q.right);
        }
        return false;
    }
}

在这里插入图片描述

4.NO.104. 二叉树的最大深度

在这里插入图片描述

/**
 * 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 int MaxDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        return Math.Max(MaxDepth(root.left)+1,MaxDepth(root.right)+1);
    }
}

在这里插入图片描述

5.NO.559. N 叉树的最大深度

在这里插入图片描述

/*
// Definition for a Node.
public class Node {
    public int val;
    public IList<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, IList<Node> _children) {
        val = _val;
        children = _children;
    }
}
*/

public class Solution {
    public int MaxDepth(Node root) {
        if(root==null){
            return 0;
        }
        int max = 0;
        foreach(Node child in root.children)
        {
            max=Math.Max(max, MaxDepth(child));
        }
        return max + 1;
    }
}

在这里插入图片描述

6.NO.111.二叉树的最小深度

在这里插入图片描述

/**
 * 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 int MinDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        //不是叶子节点,继续
        else if(root.left==null&&root.right!=null){
            return MinDepth(root.right)+1;
        }

        else if(root.left!=null&&root.right==null){
            return MinDepth(root.left)+1;
        }

        return Math.Min(MinDepth(root.left)+1,MinDepth(root.right)+1);
    }
}

在这里插入图片描述

7.NO.222. 完全二叉树的节点个数

在这里插入图片描述
无脑层序遍历法

/**
 * 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 int CountNodes(TreeNode root) {
        Queue<TreeNode> que = new Queue<TreeNode>();
        int ans=0;
        if(root==null){
            return 0;
        }
        int count=0;
        que.Enqueue(root);
        while(que.Count!=0){
            count=que.Count;
            for(int i=0;i<count;i++){
                TreeNode tmpNode = que.Dequeue();
                ans++;
                if(tmpNode.left!=null){
                    que.Enqueue(tmpNode.left);
                }
                if(tmpNode.right!=null){
                    que.Enqueue(tmpNode.right);
                }
            }
        }
        return ans;
    }
}

在这里插入图片描述
递归法:

/**
 * 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 int CountNodes(TreeNode root) {
        if(root==null){
            return 0;
        }
        return 1+CountNodes(root.left)+CountNodes(root.right);
    }
}

在这里插入图片描述

8.NO.110. 平衡二叉树

在这里插入图片描述

/**
 * 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 bool IsBalanced(TreeNode root) {
        if(getDepth(root)==-1){
            return false;
        }
        return true;
    }

    public int getDepth(TreeNode root){
        if(root==null){
            return 0;
        }
        int leftDepth=getDepth(root.left);
        int rightDepth=getDepth(root.right);
        //以-1来标记两边的长度相差超过1
        if(leftDepth==-1||rightDepth==-1||Math.Abs(rightDepth-leftDepth)>1){
            return -1;
        }
        return Math.Max(getDepth(root.left),getDepth(root.right))+1;
    }
}

在这里插入图片描述

9.NO.257. 二叉树的所有路径

在这里插入图片描述

/**
 * 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 class myPath{
        public TreeNode cur;
        public string path;
        public myPath(TreeNode cur,string path){
            this.cur = cur;
            this.path = path;
        }
    }
    
    List<string> ans= new List<string>();
    public IList<string> BinaryTreePaths(TreeNode root) {
        if(root==null){
            return ans;
        }
        getPaths(new myPath(root,root.val.ToString()));
        return ans;
    }

    public void getPaths(myPath root){
        //如果左/右节点不为空,就将左/右节点加入,并且更新路径
        if(root.cur.left!=null){
            getPaths(new myPath(root.cur.left, root.path + "->" + root.cur.left.val.ToString()));
        }
        if(root.cur.right!=null){
            getPaths(new myPath(root.cur.right, root.path + "->" + root.cur.right.val.ToString()));
        }
        if(root.cur.left==null&&root.cur.right==null){
            ans.Add(root.path);
        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值