代码随想录算法训练营第十七天|110. 平衡二叉树|257. 二叉树的所有路径| 404.左叶子之和

110. 平衡二叉树

题目:给定一个二叉树,判断它是否是高度平衡的二叉树。

本题中,一棵高度平衡二叉树定义为:

一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。

示例一:代码随想录算法训练营第十七天|110. 平衡二叉树|257. 二叉树的所有路径| 404.左叶子之和_第1张图片

输入:root = [3,9,20,null,null,15,7]
输出:true

思路:通过后续递归遍历,计算结点的左右子树的高度差。

C#递归代码:

public class Solution {
    public bool IsBalanced(TreeNode root) {
        return getHeight(root) != -1;
    }
    public int getHeight(TreeNode node)
    {
        if(node == null) return 0;
        int leftTree = getHeight(node.left);
        if(leftTree==-1) return -1;
        int rightTree =getHeight(node.right);
        if(rightTree==-1) return -1;
        
        int result = Math.Abs(leftTree - rightTree);
        if(result > 1) result = -1;
        else result = Math.Max(leftTree,rightTree) + 1; 
        return result;
    }
}

257. 二叉树的所有路径

题目:给你一个二叉树的根节点 root ,按 任意顺序 ,返回所有从根节点到叶子节点的路径。
叶子节点 是指没有子节点的节点。
示例一:代码随想录算法训练营第十七天|110. 平衡二叉树|257. 二叉树的所有路径| 404.左叶子之和_第2张图片

输入:root = [1,2,3,null,5]
输出:[“1->2->5”,“1->3”]

思路:根节点到叶子的路径,所以需要前序遍历,这样才方便让父节点指向孩子节点,找到对应的路径。

/**
 * 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 IList<string> BinaryTreePaths(TreeNode root) {
        var result = new List<string>();
        if(root == null) return result;
        var path = new List<int>();
        traversal(root,path,result);
        return result;
    }
    public void traversal(TreeNode cur,List<int> path,List<string> result)
    {
        path.Add(cur.val);
        //终止条件
        if(cur.left==null && cur.right== null){
            var sPath = new StringBuilder();
            for(int i=0;i<path.Count - 1;i++){
                sPath.Append(path[i].ToString());
                sPath.Append("->");
            }
            sPath.Append(path[path.Count - 1].ToString());
            result.Add(sPath.ToString());
            return;
        }
        //左子树
        if(cur.left != null){ 
            traversal(cur.left,path,result);
            path.RemoveAt(path.Count-1);
        }
        if(cur.right != null){
            traversal(cur.right,path,result);
            path.RemoveAt(path.Count-1);
        }
    }
}

404. 左叶子之和

题目:给定二叉树的根节点 root ,返回所有左叶子之和。
示例一:代码随想录算法训练营第十七天|110. 平衡二叉树|257. 二叉树的所有路径| 404.左叶子之和_第3张图片

输入: root = [3,9,20,null,null,15,7]
输出: 24
解释: 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24

思路:

  • 首先要注意是判断左叶子,不是二叉树左侧节点,所以不要上来想着层序遍历。
  • 明确左叶子的定义,节点A的左孩子不为空,且左孩子的左右孩子都为空(说明是叶子节点),那么A节点的左孩子为左叶子节点
  • 通过父节点来判断其左孩子是不是左叶子了

C#递归代码:

/**
 * 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 SumOfLeftLeaves(TreeNode root) {
        if (root == null) return 0;
        if(root.left == null && root.right == null) return 0;
        int leftValue = SumOfLeftLeaves(root.left);    // 左
        int rightValue = SumOfLeftLeaves(root.right);  // 右
                                                       
        int midValue = 0;
        if (root.left != null && root.left.left == null && root.left.right == null) { 
            midValue = root.left.val;
        }
        int sum = midValue + leftValue + rightValue;  // 中
        return sum;
    }
}

你可能感兴趣的:(算法,算法,数据结构)