摘要: 深度优先搜索 /** * 时间复杂度 O(n) * 空间复杂度 O(logn) */class Solution { public int maxDepth(Node root) { if (root == null){ return 0; } int max = 0; for (Node c : 阅读全文
posted @ 2022-02-21 17:54 振袖秋枫问红叶 阅读(26) 评论(0) 推荐(0)
摘要: 深度优先搜索 class Solution { public boolean isSubtree(TreeNode root, TreeNode subRoot) { /** * 对于树的每个节点,都判断一下是否和子树是一个树(《100. 相同的树》) */ if (subRoot == null) 阅读全文
posted @ 2022-02-21 17:11 振袖秋枫问红叶 阅读(23) 评论(0) 推荐(0)
摘要: 深度优先搜索 class Solution { List<Integer> list = new LinkedList<>(); public List<Integer> postorder(Node root) { if (root == null){ return list; } for (No 阅读全文
posted @ 2022-02-21 14:52 振袖秋枫问红叶 阅读(21) 评论(0) 推荐(0)
摘要: 深度优先搜索 class Solution { List<Integer> list = new LinkedList<>(); public List<Integer> preorder(Node root) { if (root == null){ return list; } list.add 阅读全文
posted @ 2022-02-21 14:40 振袖秋枫问红叶 阅读(19) 评论(0) 推荐(0)
摘要: 广度优先搜索 class Solution { public Node connect(Node root) { Queue<Node> queue = new LinkedList<>(); if (root == null){ return root; } queue.add(root); wh 阅读全文
posted @ 2022-02-21 13:54 振袖秋枫问红叶 阅读(25) 评论(0) 推荐(0)
摘要: 广度优先搜索 class Solution { public Node connect(Node root) { Queue<Node> queue = new LinkedList<>(); if (root == null){ return root; } queue.add(root); wh 阅读全文
posted @ 2022-02-21 13:50 振袖秋枫问红叶 阅读(26) 评论(0) 推荐(0)
摘要: 广度优先搜索 class Solution { public List<Integer> largestValues(TreeNode root) { LinkedList<Integer> list = new LinkedList<>(); Queue<TreeNode> queue = new 阅读全文
posted @ 2022-02-21 13:05 振袖秋枫问红叶 阅读(30) 评论(0) 推荐(0)
摘要: 广度优先搜索 class Solution { public List<List<Integer>> levelOrder(Node root) { List<List<Integer>> list = new LinkedList<>(); Queue<Node> queue = new Link 阅读全文
posted @ 2022-02-21 12:05 振袖秋枫问红叶 阅读(29) 评论(0) 推荐(0)
摘要: 广度优先搜索 class Solution { public List<Double> averageOfLevels(TreeNode root) { List<Double> list = new LinkedList<>(); Queue<TreeNode> queue = new Linke 阅读全文
posted @ 2022-02-21 11:46 振袖秋枫问红叶 阅读(23) 评论(0) 推荐(0)