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

#LeetCode 110. Balanced Binary Tree

#LeetCode 110. 视频讲解:后序遍历求高度,高度判断是否平衡 | LeetCode:110.平衡二叉树_哔哩哔哩_bilibili

平衡二叉树的定义是:二叉树的每个节点的左右子树的高度之差不超过1.

如果计算二叉树的高度用后序遍历,二叉树的深度用前序遍历,原因如下:

二叉树的高度:叶子节点所在的层为1,所以是汇总左右孩子情况后 + 1,返回给父节点,层层向上返回,所以是左右中——后序遍历。

二叉树的深度:根节点所在的层为1,逐步向下递增,符合层层向下的逻辑,所以是中左右——前序遍历。

递归三部曲:

1. 考虑参数和返回值:参数是节点、返回值为int(高度值)。 

2. 终止条件:如果node == null,则代表到了末端节点。

3. 单层递归逻辑

用后序遍历才可以在得到左右子树情况后再比较,而且需要根据左右子树情况返回给父节点。

递归方法代码:

/**
 * 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 {
    public boolean isBalanced(TreeNode root) {
        int height = getHeight(root);
        return height == -1 ? false : true;
    }
    public int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = getHeight(root.left);
        if (leftHeight == -1) {
            return -1;
        }
        int rightHeight = getHeight(root.right);
        if (rightHeight == -1) {
            return -1;
        }
        int result = 0;
        if (Math.abs(leftHeight - rightHeight) > 1) {
            return -1;
        }
        else {result = Math.max(leftHeight, rightHeight) + 1;}
        return result;
    }
}

#LeetCode257. Binary Tree Path

#LeetCode 257. 视频讲解:递归中带着回溯,你感受到了没?| LeetCode:257. 二叉树的所有路径_哔哩哔哩_bilibili

递归三部曲:

1. 考虑参数和返回值:参数是节点、返回值为List result。 

2. 终止条件:当current node 不为空,但是左右孩子节点都为空时代表找到了叶子节点。

3. 单层递归逻辑:按照前序遍历的顺序读入父节点--左节点--右节点。

在这里会用到递归和回溯,回溯的过程是通过在List 中remove 最后一个节点即为回到了上一个节点。

 前序遍历代码:

/**
 * 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 {
    public List binaryTreePaths(TreeNode root) {
        List result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        List path = new ArrayList<>();
        traversal(root, path, result);
        return result;
    }
    public void traversal (TreeNode cur, List path, List result) {
        path.add(cur.val);
        // leaf node
        if (cur.left == null && cur.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));
            result.add(sb.toString());
            return;
        }
        if (cur.left != null) {
            traversal(cur.left, path, result);
            path.remove(path.size() - 1); // backtrack
        }
        if (cur.right != null) {
            traversal(cur.right, path, result);
            path.remove(path.size() - 1);
        }
    }
}

#LeetCode404. Sum of Left Leaves

#LeetCode 404. 视频讲解:二叉树的题目中,总有一些规则让你找不到北 | LeetCode:404.左叶子之和_哔哩哔哩_bilibili

左叶子的定义是左右孩子为空且是父节点的左孩子。

用后序遍历来一层一层向上返回,在根节点汇总左右子树的左叶子节点的和。

if (root.left == null && root.right == null) 代表遍历到了叶子节点,return 0, 回到上一节点来计算。

后序遍历代码:

/**
 * 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 {
    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 0;
        }
        int leftNum = sumOfLeftLeaves(root.left);
        if (root.left != null && root.left.left == null && root.left.right == null) {
            leftNum = root.left.val;
        }
        int rightNum = sumOfLeftLeaves(root.right);
        int sum = leftNum + rightNum;
        return sum;
    }
}

你可能感兴趣的:(算法)