美文网首页
算法|平衡二叉树、二叉树的所有路径、 左叶子之和

算法|平衡二叉树、二叉树的所有路径、 左叶子之和

作者: 激扬飞雪 | 来源:发表于2022-12-01 16:15 被阅读0次

    一、 110. 平衡二叉树

    题目链接:https://leetcode.cn/problems/balanced-binary-tree/
    思路:使用后序遍历求每个节点的高度,高度差的绝对值大于1 就返回-1 ,反之则返回最大高度 + 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 {
        private 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;
            if (Math.abs(leftHeight - rightHeight) > 1) {
                return -1;
            }
            return Math.max(leftHeight, rightHeight) + 1;
        }
        public boolean isBalanced(TreeNode root) {
            return getHeight(root) != -1;
        }
    }
    

    二、 257. 二叉树的所有路径

    题目链接:https://leetcode.cn/problems/binary-tree-paths/
    思路一:使用前序遍历,到root.left == null && root.right == null 搜集集合

    /**
     * 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 {
        private void backTrack(TreeNode root, List<Integer> paths, List<String> result) {
            paths.add(root.val);
            if (root.left == null && root.right == null) {
                //收集结果
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < paths.size(); i++) {
                    stringBuilder.append(paths.get(i));
                    if (i != paths.size() - 1) {
                        stringBuilder.append("->");
                    }
                }
                result.add(stringBuilder.toString());
                return;
            }
            if (root.left != null) {
                backTrack(root.left, paths, result);
                //回溯
                paths.remove(paths.size() - 1);
            }
            if (root.right != null) {
                backTrack(root.right, paths, result);
                //回溯
                paths.remove(paths.size() - 1);
            }
        }
        public List<String> binaryTreePaths(TreeNode root) {
            List<Integer> paths = new ArrayList<>();
            List<String> result = new ArrayList<>();
            if (root == null) return result; 
            backTrack(root, paths, result); 
            return result;
        }
    }
    

    思路二、使用前序遍历 迭代法 使用两个栈 一个栈用于存节点,另外一个节点存路径,到达叶子节点后,搜集结果

    /**
     * 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<String> binaryTreePaths(TreeNode root) {
            List<String> result = new ArrayList<>();
            if (root == null) return result;
            Stack<TreeNode> stackNode = new Stack<>();
            Stack<String>   stackPath = new Stack<>();
            stackNode.push(root);
            stackPath.push(root.val + "");
            while (!stackNode.isEmpty()) {
                TreeNode treeNode = stackNode.pop();
                String path = stackPath.pop();
                if (treeNode.left == null && treeNode.right == null) {
                    result.add(path);
                    continue;
                }
    
                if (treeNode.right != null) {
                    stackNode.push(treeNode.right);
                    stackPath.push(path + "->" + treeNode.right.val);
                }
                if (treeNode.left != null) {
                    stackNode.push(treeNode.left);
                    stackPath.push(path + "->" + treeNode.left.val);
                }
            
            }
            return result;
        }
    }
    

    三、404. 左叶子之和

    题目链接:https://leetcode.cn/problems/sum-of-left-leaves/
    思路一:使用后序遍历

    /**
     * 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;
            int leftSum = sumOfLeftLeaves(root.left);
            int rightSum = sumOfLeftLeaves(root.right);
            int midSum = 0;
            if (root.left != null && root.left.left == null && root.left.right == null) {
                midSum = root.left.val;
            }
            return leftSum + midSum + rightSum;
        }
    }
    

    思路二:使用迭代前序遍历

    /**
     * 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;
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root);
            int sum = 0;
            while (!stack.isEmpty()) {
                TreeNode treeNode = stack.pop();
                if (treeNode.left != null
                 && treeNode.left.left == null 
                 && treeNode.left.right == null) {
                     sum += treeNode.left.val;
                 }
                 if (treeNode.right != null) stack.push(treeNode.right);
                 if (treeNode.left != null) stack.push(treeNode.left);
            }
            return sum;
        }
    }
    

    相关文章

      网友评论

          本文标题:算法|平衡二叉树、二叉树的所有路径、 左叶子之和

          本文链接:https://www.haomeiwen.com/subject/mzbefdtx.html