Leetcode 一些简单的二叉树

二叉树的中序遍历

 按照访问左子树——根节点——右子树的方式遍历这棵树.

定义 inorder(root) 表示当前遍历到 \textit{root}root 节点的答案,那么按照定义,我们只要递归调用 inorder(root.left) 来遍历 \textit{root}root 节点的左子树,然后将 \textit{root}root 节点的值加入答案,再递归调用inorder(root.right) 来遍历 \textit{root}root 节点的右子树即可,递归终止的条件为碰到空节点。

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        inorder(root, res);
        return res;
    }

    public void inorder(TreeNode root, List<Integer> res) {
        if (root == null) {
            return;
        }
        inorder(root.left, res);
        res.add(root.val);
        inorder(root.right, res);
    }
}

附加前序遍历代码

class Solution {
        public List<Integer> preorderTraversal(TreeNode root) {
            List<Integer> res = new ArrayList<Integer>();
            preorder(root,res);
            return res;
        }
        public void preorder(TreeNode root, List<Integer> res){
            if(root == null){
                return ;
            }
            res.add(root.val);
            preorder(root.left,res);
            preorder(root.right,res);
        }
    }

附加后序遍历代码

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        postorder(root, res);
        return res;
    }

    public void postorder(TreeNode root, List<Integer> res) {
        if (root == null) {
            return;
        }
        postorder(root.left, res);
        postorder(root.right, res);
        res.add(root.val);
    }
}

相同的树

class Solution {
        public boolean isSameTree(TreeNode p, TreeNode q) {
            if(p==null&&q==null)
                return true;
            else if(p==null || q==null)
                return false;
            else if(p.val!=q.val)
                return false;
            else
                return isSameTree(p.left,q.left)&& isSameTree(p.right,q.right);
        }
    }

对称二叉树

class Solution {
        public boolean isSymmetric(TreeNode root) {
            return check(root,root);
        }
        public boolean check(TreeNode p,TreeNode q){
            if(p==null&&q==null)
                return true;
            else if(p==null || q==null)
                return false;
            else
                return p.val==q.val && check(p.left,q.right) && check(p.right,q.left);
        }
    }

平衡二叉树


 

class Solution {
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        } else {
            return Math.abs(height(root.left) - height(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
        }
    }

    public int height(TreeNode root) {
        if (root == null) {
            return 0;
        } else {
            return Math.max(height(root.left), height(root.right)) + 1;
        }
    }
}

二叉树的最大深度

class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        } else {
            int leftHeight = maxDepth(root.left);
            int rightHeight = maxDepth(root.right);
            return Math.max(leftHeight, rightHeight) + 1;
        }
    }
}

附加最小深度

class Solution {
    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }

        if (root.left == null && root.right == null) {
            return 1;
        }

        int min_depth = Integer.MAX_VALUE;
        if (root.left != null) {
            min_depth = Math.min(minDepth(root.left), min_depth);
        }
        if (root.right != null) {
            min_depth = Math.min(minDepth(root.right), min_depth);
        }

        return min_depth + 1;
    }
}

路径总和

class Solution {
        public boolean hasPathSum(TreeNode root, int targetSum) {
            if(root==null)
                return false;
            if(root.left==null&&root.right==null)
                return targetSum==root.val;
            return hasPathSum(root.left,targetSum-root.val)||hasPathSum(root.right,targetSum-root.val);

        }
    }

猜你喜欢

转载自blog.csdn.net/m0_61897853/article/details/125267487