代码随想录-二叉树

************二叉树的前序遍历:

. - 力扣(LeetCode)

递归


public class LeetCode144 {
    class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode() {
        }

        public TreeNode(int val) {
            this.val = val;
        }

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res=new LinkedList<>();
        preOrder(root,res);
        return res;
    }

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

}

迭代:

 

/**
 * 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<Integer> preorderTraversal(TreeNode root) {
        LinkedList<TreeNode> stack=new LinkedList<>();
        List<Integer> res=new LinkedList<>();
        if (root==null){
            return res;
        }
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode pop = stack.pop();
            res.add(pop.val);
            if (pop.right!=null) stack.push(pop.right);
            if (pop.left!=null) stack.push(pop.left);
        }
        return res;
    }
}
二叉树的后续遍历:

. - 力扣(LeetCode)


public class LeetCode145 {
    class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode() {
        }

        public TreeNode(int val) {
            this.val = val;
        }

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list=new LinkedList<>();
        postOrder(root,list);
        return list;
    }

    private void postOrder(TreeNode root, List<Integer> list) {
        if (root==null){
            return;
        }
        if (root.left!=null){
            postOrder(root.left,list);
        }
        if (root.right!=null){
            postOrder(root.right,list);
        }
        list.add(root.val);
    }

}

迭代:不太理解

/**
 * 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<Integer> postorderTraversal(TreeNode root) {
       LinkedList<TreeNode> stack=new LinkedList<>();
        List<Integer> res=new LinkedList<>();
        if (root==null){
            return res;
        }
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode pop = stack.pop();
            res.add(pop.val);
            if (pop.left!=null) stack.push(pop.left);
            if (pop.right!=null) stack.push(pop.right);
        }
        Collections.reverse(res);
        return res;
    }
}
二叉树的中序遍历

. - 力扣(LeetCode)

/**
 * 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<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res=new LinkedList<>();
        LinkedList<TreeNode> stack=new LinkedList<>();
        TreeNode cur=root;
        while (cur!=null||!stack.isEmpty()){
            if (cur!=null){
                stack.push(cur);
                cur=cur.left;
            }else{
                cur = stack.pop();
                res.add(cur.val);
                cur=cur.right;
            }
        }
        return res;
    }
}
****二叉树的最小深度:
 

. - 力扣(LeetCode)


 

/**
 * 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 minDepth(TreeNode root) {
        //只有当左右孩子都为null时才算
        if (root==null) return 0;
        if (root.left==null&&root.right==null) return 1;
        int min=Integer.MAX_VALUE;
        if (root.left!=null) min=Math.min(min,minDepth(root.left));
        if (root.right!=null) min=Math.min(min,minDepth(root.right));
        return 1+min;
    }
}
翻转二叉树:

. - 力扣(LeetCode)

递归:可以使用递归的大多都可以使用栈进行迭代

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;
      }
  }
    public TreeNode invertTree(TreeNode root) {
        if (root==null) return root;
        if (root.left==null&&root.right==null) return root;
        TreeNode temp=root.left;
        root.left=root.right;
        root.right=temp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }

迭代:

public TreeNode invertTree2(TreeNode root) {
        if (root==null) return root;
        if (root.left==null&&root.right==null) return root;
        LinkedList<TreeNode> stack=new LinkedList<>();
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode pop = stack.pop();
            TreeNode temp=pop.left;
            pop.left=pop.right;
            pop.right=temp;
            if (pop.left!=null) stack.push(pop.left);
            if (pop.right!=null) stack.push(pop.right);
        }
        return root;
    }

相关推荐

  1. 代码随想》--

    2024-04-15 00:50:02       62 阅读
  2. 代码随想-

    2024-04-15 00:50:02       39 阅读
  3. 代码随想刷——day15

    2024-04-15 00:50:02       43 阅读
  4. 代码随想刷——day18

    2024-04-15 00:50:02       60 阅读
  5. 代码随想刷——day22

    2024-04-15 00:50:02       57 阅读
  6. 代码随想 96. 不同的搜索

    2024-04-15 00:50:02       57 阅读
  7. 代码随想第22天|

    2024-04-15 00:50:02       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-15 00:50:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-15 00:50:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-15 00:50:02       87 阅读
  4. Python语言-面向对象

    2024-04-15 00:50:02       96 阅读

热门阅读

  1. C++ 中的运算符优先级

    2024-04-15 00:50:02       35 阅读
  2. 每日练习——leetcode454和4

    2024-04-15 00:50:02       41 阅读
  3. leetcode48 旋转图像

    2024-04-15 00:50:02       43 阅读
  4. Kotlin 面试题

    2024-04-15 00:50:02       32 阅读
  5. 前端面试问题汇总 - 工程管理工具篇

    2024-04-15 00:50:02       43 阅读
  6. 前端面试问题汇总 - 其他问题

    2024-04-15 00:50:02       42 阅读
  7. c++智能指针(2) -- auto_ptr

    2024-04-15 00:50:02       39 阅读
  8. Elasticsearch 支持的插件 —— 筑梦之路

    2024-04-15 00:50:02       42 阅读