数据结构每日学习 Day1 二叉树
2022-01-02 22:47
1006 查看
第一题:
114. 二叉树展开为链表
解题:
首先回顾一下 二叉树的递归遍历和迭代遍历
递归遍历如下:
(三种遍历方式只需调整add语句位置即可)
public void dfs(TreeNode root,List<TreeNode> list){ if (root!=null){ list.add(root); dfs(root.left,list); dfs(root.right,list); } }
迭代遍历+Morris解法如下:
https://leetcode-cn.com/problems/binary-tree-preorder-traversal/solution/leetcodesuan-fa-xiu-lian-dong-hua-yan-shi-xbian-2/
中序迭代(自认为好理解的):
public static void inOrderIteration(TreeNode head) { if (head == null) { return; } TreeNode cur = head; Stack<TreeNode> stack = new Stack<>(); while (!stack.isEmpty() || cur != null) { while (cur != null) { stack.push(cur); cur = cur.left; } TreeNode node = stack.pop(); System.out.print(node.value + " "); if (node.right != null) { cur = node.right; } } }
前序迭代:(这个画个图 理解一下就很快)
public static void preOrderIteration(TreeNode head) { if (head == null) { return; } Stack<TreeNode> stack = new Stack<>(); stack.push(head); while (!stack.isEmpty()) { TreeNode node = stack.pop(); System.out.print(node.value + " "); if (node.right != null) { stack.push(node.right); } if (node.left != null) { stack.push(node.left); } } }
感觉下面这个更好理解一些
Deque<TreeNode> stack = new LinkedList<TreeNode>(); TreeNode node = root; while (node != null || !stack.isEmpty()) { while (node != null) { list.add(node); stack.push(node); node = node.left; } node = stack.pop(); node = node.right; }
AC:
package 二叉树; import java.util.ArrayList; import java.util.List; public class 二叉树展开为链表 { 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; } } /* Queue<TreeNode> queue = new ArrayDeque<>(); preOrder(root, queue); while (!queue.isEmpty()){ TreeNode curr = queue.poll(); curr.left = null; curr.right = queue.peek(); } } */ public void flatten(TreeNode root) { List<TreeNode> list=new ArrayList<TreeNode>(); dfs(root,list); int len=list.size(); for (int i = 1; i <len ; i++) { //原本下面这一行错了 注意理解 TreeNode prev = list.get(i - 1), curr = list.get(i); prev.left=null; prev.right=curr; } } public void dfs(TreeNode root,List<TreeNode> list){ if (root!=null){ list.add(root); dfs(root.left,list); dfs(root.right,list); } } }
第二题:543. 二叉树的直径
AC:(太妙了 首先你要会深度是怎么求的 !!! )(这个递归过程建议反复咀嚼)
class Solution { int ans; public int diameterOfBinaryTree(TreeNode root) { ans = 1; depth(root); return ans - 1; } public int depth(TreeNode node) { if (node == null) { return 0; // 访问到空节点了,返回0 } int L = depth(node.left); // 左儿子为根的子树的深度 int R = depth(node.right); // 右儿子为根的子树的深度 ans = Math.max(ans, L+R+1); // 计算d_node即L+R+1 并更新ans return Math.max(L, R) + 1; // 返回该节点为根的子树的深度 } }
相关文章推荐
- 一步一步学习数据结构(4)-树和二叉树基础知识
- 数据结构学习(C++)—二叉树
- 每日学习分享Day1
- 数据结构 学习笔记(四):树(上):树的表示,二分查找,二叉树,先中后层次遍历
- 【学习笔记----数据结构10-二叉树】
- 【数据结构】二叉树的原理及实现学习总结
- 数据结构学习笔记——二叉树的存储结构
- 数据结构学习笔记-二叉树的层序遍历
- 数据结构学习笔记 --- 二叉树
- 算法学习总结(3)——二叉树数据结构重点知识总结
- Java源码集合类TreeMap学习1——数据结构3二叉树创建代码
- 数据结构学习笔记——二叉树的类型定义
- 数据结构学习之二叉树
- 数据结构学习笔记:二叉树
- 【数据结构学习】-二叉树的概念与遍历
- 学习C#泛型概述,一个二叉树的数据结构
- 【数据结构学习笔记】——根据中缀表达式构建二叉树并输出
- 数据结构学习:利用链表建立二叉树
- 数据结构学习笔记 --- 二叉树
- 数据结构学习之二叉树(面试易考题整理)