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

105 阅读2分钟

110. Balanced Binary Tree

Given a binary tree, determine if it is 

height-balanced

.

 

Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: true

Example 2:

Input: root = [1,2,2,3,3,null,null,4,4]
Output: false

Example 3:

Input: root = []
Output: true

 

Constraints:

  • The number of nodes in the tree is in the range [0, 5000].
  • -104 <= Node.val <= 104

判断是否为平衡二叉树。那么就要在每一个父节点判断左右子树的深度差是否大于1。 用递归返回深度,在父节点判断左右深度差值即可。使用一个全局变量保存比较结果。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    boolean mbal = true;
    public int rcHelper(TreeNode root) {
        if(root == null) {
            return 0;
        }

        int ldepth = rcHelper(root.left);
        int rdepth = rcHelper(root.right);

        if(Math.abs(ldepth - rdepth) > 1) {
            mbal = false;
        }

        return Math.max(ldepth + 1, rdepth + 1);
    }
    public boolean isBalanced(TreeNode root) {
        if(root == null) {
            return true;
        }

        rcHelper(root);

        return mbal;
    }
}

257. Binary Tree Paths

Given the root of a binary tree, return all root-to-leaf paths in any order.

leaf is a node with no children.

 

Example 1:

Input: root = [1,2,3,null,5]
Output: ["1->2->5","1->3"]

Example 2:

Input: root = [1]
Output: ["1"]

 

Constraints:

  • The number of nodes in the tree is in the range [1, 100].
  • -100 <= Node.val <= 100

返回所有二叉树路径。这题主要是java不熟。递归函数一个变量保存遍历节点,一个变量保存路径,还需要第三个来保存最终结果。 在遍历到叶子节点时,处理路径,保存结果。

class Solution {
    public void rcHelper(TreeNode root, List<Integer> path, List<String> res) {
        path.add(root.val);
        if(root.left == null && root.right == null) {
            StringBuilder sb = new StringBuilder();

            for(int i=0; i<path.size()-1; i++) {
                sb.append(path.get(i)).append("->");
            }
            sb.append(path.get(path.size() - 1));
            res.add(sb.toString());
            return;
        }

        if(root.left != null) {
            rcHelper(root.left, path, res);
            path.remove(path.size() - 1);
        }

        if(root.right != null) {
            rcHelper(root.right, path, res);
            path.remove(path.size() - 1);
        }
    }

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> ans = new LinkedList<String>();
        List<Integer> path = new LinkedList<Integer>();
        List<List<Integer>> ilist = new LinkedList<List<Integer>>();

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

        rcHelper(root, path, ans);

        return ans;
    }
}

404. Sum of Left Leaves

Given the root of a binary tree, return the sum of all left leaves.

leaf is a node with no children. A left leaf is a leaf that is the left child of another node.

 

Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: 24
Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively.

Example 2:

Input: root = [1]
Output: 0

 

Constraints:

  • The number of nodes in the tree is in the range [1, 1000].
  • -1000 <= Node.val <= 1000

求所有左叶子节点的和。叶子结点自己不可能知道自己是左还是右,只能从父节点开始判断。 那么在父节点的遍历中,就要加入左右是否叶子节点的判断,若存在左叶子结点,和加上左叶子结点的值,若右边是叶子结点,则不加。 用的是前序遍历。先往左下走,得到左子树的左叶子节点数值的和,但在左边为叶子节点时,左子树的和值就是左叶子节点的值。然后右边递归。最后左右相加返回。

class Solution {
    Integer msum = 0;

    public int rcHelper(TreeNode root) {
        if(root == null) {
            return 0;
        }

        if(root.left == null && root.right == null) {
            return 0;
        }
        
        int lc = rcHelper(root.left);
        if(root.left != null && root.left.left == null && root.left.right == null) {
            lc = root.left.val;
        }
        int rc = rcHelper(root.right);

        return lc + rc;
    }
    public int sumOfLeftLeaves(TreeNode root) {
        return rcHelper(root);
    }
}