左程云基础班——二叉树

1. 遍历二叉树

二叉树结点:

public class Node {public int value;public Node left;public Node right;public Node(int value) {this.value = value;}
}

1)前序遍历

 public static void preOrderRecur(Node head) {if (head == null) {return;}System.out.print(head.value + " ");preOrderRecur(head.left);preOrderRecur(head.right);}public static void preOrderUnRecur(Node head) {if (head != null) {//入栈顺序根右左Stack<Node> stack = new Stack<>();stack.push(head);while (!stack.isEmpty()) {head = stack.pop();System.out.print(head.value + " ");if (head.right != null) {stack.push(head.right);}if (head.left != null) {stack.push(head.left);}}}}

2)后序遍历

 public static void postOrderRecur(Node head) {if (head == null) {return;}postOrderRecur(head.left);postOrderRecur(head.right);System.out.print(head.value + " ");}//入栈顺序根左右,逆序输出public static void postOrderUnRrecur(Node head) {if (head != null) {Stack<Node> stack = new Stack<>();Stack<Node> res = new Stack<>();stack.push(head);while (!stack.isEmpty()) {head = stack.pop();res.push(head);if (head.left != null) {stack.push(head.left);}if (head.right != null) {stack.push(head.right);}}while (!res.isEmpty()) {System.out.print(res.pop().value + " ");}}}

3)中序遍历

 public static void inOrderRecur(Node head) {if (head == null) {return;}inOrderRecur(head.left);System.out.print(head.value + " ");inOrderRecur(head.right);}//左子树全部入栈public static void inOrderUnRecur(Node head) {if (head != null) {Stack<Node> stack = new Stack<>();while (!stack.isEmpty() || head != null) {if (head != null) {stack.push(head);head = head.left;} else {head = stack.pop();System.out.print(head.value + " ");head = head.right;}}}}

4)宽度遍历(最大宽度)

public static int WildTravel(Node head) {if (head != null) {LinkedList<Node> queue = new LinkedList<>();HashMap hashMap = new HashMap<Node, Integer>();hashMap.put(head, 1);int curlevel = 0;int levelNodes = 0;int maxNodes = 0;queue.add(head);while (!queue.isEmpty()) {head = queue.poll();//                System.out.print(head.value + " ");if ((int)hashMap.get(head) > curlevel) {curlevel = (int)hashMap.get(head);levelNodes = 1;}else {levelNodes++;}maxNodes = Math.max(maxNodes, levelNodes);if (head.left != null) {hashMap.put(head.left, (int)hashMap.get(head) + 1);queue.add(head.left);}if (head.right != null) {hashMap.put(head.right, (int)hashMap.get(head) + 1);queue.add(head.right);}}return maxNodes;}return 0;}

2. 判断树结构

树形DP的套路方法:

根据左树的信息和右树的信息得到自己的信息

1)判断二叉查找树

递归实现:

    public static int preValue = Integer.MIN_VALUE;public static boolean checkBST1(Node head) {if (head == null) {return true;}boolean left = checkBST1(head.left);if (left != true) {return false;}if (head.value < preValue) {return false;}else {preValue = head.value;}return checkBST1(head.right);}

非递归实现:

 public static boolean checkBST2(Node head) {if (head != null) {int preValue = Integer.MIN_VALUE;Stack<Node> stack = new Stack<>();while (!stack.isEmpty() || head != null) {if (head != null) {stack.push(head);head = head.left;} else {head = stack.pop();if (head.value < preValue) {return false;}else {preValue = head.value;}head = head.right;}}}return true;}

套路实现:

 public static boolean checkBST3(Node head) {bData avData = chBST(head);return avData.isBST;}private static class bData {//每个结点返回三个信息public boolean isBST;public int max;public int min;public bData(boolean isBST, int max, int min) {this.isBST = isBST;this.max = max;this.min = min;}}public static bData chBST(Node head) {//边界信息不好确定,返回空//确定自己信息时再具体判断if (head == null) {return null;}bData left = chBST(head.left);bData right = chBST(head.right);int min = head.value;int max = head.value;if (left != null) {min = Math.min(min, left.min);max = Math.max(max, left.max);}if (right != null) {min = Math.min(min, right.min);max = Math.max(max, right.max);}boolean isBST = true;if (left != null && (!left.isBST || left.min > head.value)) {isBST = false;}if (right != null && (!right.isBST || right.max < head.value)) {isBST = false;}return new bData(isBST, max, min);}

2). 判断完全二叉树

public static boolean checkCBT(Node head) {if (head != null) {boolean leaf = false;//表示以后的结点都是叶结点LinkedList<Node> queue = new LinkedList<>();queue.add(head);//基于宽度遍历while (!queue.isEmpty()) {head = queue.poll();if ((head.left == null && head.right != null)|| (leaf == true && (head.left != null || head.right != null))) {return false;}if (head.left != null) {queue.add(head.left);}if (head.right != null) {queue.add(head.right);}//此结点右树为空,以后都是叶结点else {leaf = true;}}}return true;}

3). 判断满二叉树

 public static boolean checkFBT(Node head) {fData c = prosses(head);if (c.nodes == (1 << c.height) - 1)return true;elsereturn false;}private  static class fData{public int height;public int nodes;public fData(int height, int nodes) {this.height = height;this.nodes = nodes;}}public static fData prosses(Node head) {//套路解法if (head == null) {return new fData(0,0);}fData leftData = prosses(head.left);fData rightData = prosses(head.right);int height = Math.max(leftData.height, rightData.height) + 1;int nodes = leftData.nodes + rightData.nodes + 1;return new fData(height, nodes);}

3. 练习题

1). 寻找最近的公共父节点

 public static Node seekRoot(Node head, Node o1, Node o2) {HashMap<Node, Node> root = new HashMap<>();root.put(head, head);storeRoot(head, root);HashSet<Node> set = new HashSet<>();Node cur = o1;while (cur != head) {set.add(cur);cur = root.get(cur);}cur = o2;while (cur != head) {if (set.contains(cur)) {return cur;}cur = root.get(cur);}return head;}public static void storeRoot(Node head, HashMap<Node, Node> root) {if (head == null) {return;}root.put(head.left, head);root.put(head.right, head);storeRoot(head.left, root);storeRoot(head.right, root);}

2). 微软折纸面试题

 //无实体树结构的树形遍历public static void paperFolding(int N) {printPaper(1, N, true);}//i是迭代条件, N是边界, f标志左右子树private static void printPaper(int i, int N, boolean f) {if (i > N) {return;}printPaper(i + 1, N, true);String s = f == true ? "凹" : "凸";System.out.print(s + " ");printPaper(i + 1, N, false);}

3). 序列化与反序列化(左神代码)

看懵了

 public static String serializeBT(Node head) {//边界信息if (head == null) {return "#_";}String res = head.value + "_";res += serializeBT(head.left);res += serializeBT(head.right);return res;}   //反序列化二叉树public static Node deserializeBT(String res) {LinkedList<String> strings = stringsToQueue(res);Node node = deserialzeProcess(strings);return node;}private static LinkedList<String> stringsToQueue(String s) {String[] strs = s.split("_");LinkedList<String> queue = new LinkedList<>();for (String s1 : strs) {queue.add(s1);}return queue;}private static Node deserialzeProcess(LinkedList<String> queue) {String value = queue.poll();if (value.equals("#")) {return null;}Node node = new Node(Integer.valueOf(value));node.left = deserialzeProcess(queue);node.right = deserialzeProcess(queue);return node;}

左程云基础班——二叉树相关推荐

  1. 一周刷爆LeetCode,算法da神左神(左程云)耗时100天打造算法与数据结构基础到高级全家桶教程,直击BTAJ等一线大厂必问算法面试题真题详解 笔记

    一周刷爆LeetCode,算法大神左神(左程云)耗时100天打造算法与数据结构基础到高级全家桶教程,直击BTAJ等一线大厂必问算法面试题真题详解 笔记 教程与代码地址 P1 出圈了!讲课之外我们来聊聊 ...

  2. 左程云算法笔记总结-基础篇

    基础01(复杂度.基本排序) 认识复杂度和简单排序算法 时间复杂度 big O 即 O(f(n)) 常数操作的数量写出来,不要低阶项,只要最高项,并且不要最高项的系数 一个操作如果和样本的数据量没有关 ...

  3. 数据结构与算法XS班-左程云第八节课笔记(归并排序和快速排序)

    第8节 归并排序和快速排序 ##这是数据结构与算法新手班-左程云第八节课的笔记## 归并排序 归并排序实际上是一个很经典的排序方法,时间复杂度o(N*logN). 递归版本(图解排序算法(四)之归并排 ...

  4. 数据结构与算法XS班-左程云第一节课笔记(位运算、算法是什么、简单排序)

    第1节 位运算.算法是什么.简单排序 ##这是数据结构与算法新手班-左程云第一节课的笔记## 1. 位运算 // 你们会不会表示一个数字的32位啊? // Java中int类型默认以32位二进制数在计 ...

  5. CSDN专访左程云,算法之道

    算法的庞大让很多人畏惧,程序员如何正确的学习并应用于面试.工作中呢?今天,CSDN邀请了IBM软件工程师.百度软件工程师.刷题5年的算法热爱者左程云,来担任CSDN社区问答栏目的第二十六期嘉宾,届时会 ...

  6. 一看“左程云:200道算法与数据结构”,二刷“阿里云:70+算法题、30种大厂笔试高频知识点”,3月过去终于挺进我梦中的字节!

    不管是学生还是已经工作的人,我想彼此都有一个相同的梦想:进大厂! 眼看着2020年还有个三十来天就要完美收尾了,那么如何才能在未来三个月弯道超车赶上"金三银四的春招",进入梦寐以求 ...

  7. 左程云:程序员该如何学习算法?

    大家好,我是左程云.我本科就读于华中科技大学.硕士毕业于在芝加哥大学.先后在IBM.百度.GrowingIO和亚马逊工作,是一个刷题7年的算法爱好者. 我是<程序员代码面试指南--IT名企算法与 ...

  8. 左程云算法笔记(四)哈希表和有序表的使用、链表

    左程云算法笔记(四) 哈希表的使用 有序表的使用 链表 单链表反转 (LC206) 双向链表反转 打印两个有序链表的公共部分 合并两个有序链表(LC21) 判断一个链表是否为回文结构 (LC234) ...

  9. LeetCode左程云算法课笔记

    左程云算法课笔记 剑指Offer 位运算 ^运算符理解 寻找出现双中的单数 取出一个数最右边1的位置 找所有双出现中的两个单数 整数二进制奇数位偶数位交换 数组中全部出现k次返回出现一次的数 链表 判 ...

最新文章

  1. linux驱动:音频驱动(一)ALSA
  2. hash是线程安全的吗?怎么解决?_这次进程、线程、多线程和线程安全问题,一次性帮你全解决了...
  3. brad wu_一百万归功于Brad Traversy
  4. 把运维和开发放一起就是DevOps?还差得远!
  5. python发带附件的中文邮件
  6. 解决“Internet Explorer 无法打开 Internet站点已终止操作”问题(转)
  7. 做中国女人难,做中国女装更难
  8. iOS实现简书的账号识别方式(正则表达式)
  9. python中输出语句的怎么写_python的输出语句怎么写
  10. CFS三层靶机搭建及其内网渗透
  11. Word - 修改界面语言和校对语言
  12. Adobe Premiere基础-编辑素材文件常规操作(脱机文件,替换素材,素材标签和编组,素材启用,便捷调节不透明度,项目打包)(十七)
  13. opensource项目_2020 Opensource.com夏季阅读列表
  14. java批量生成条形码图片 打包zip
  15. Unity中location和rotation赋值和更改
  16. 一个人靠不靠谱,在于能力是否配得上承诺
  17. 回忆录——一份曾经面试“网易AI产品经理”的作品
  18. !EOF简单说明,常用来结束while循环
  19. SpringCloud入门 —— SSO 单点登录
  20. 【OpenCV-Python】教程:3-16 利用Grabcut交互式前景提取

热门文章

  1. ReFi夏季升温:Uniswap v3和绿色资产池在Celo上启动
  2. RF新手常见问题总结
  3. 如何解决Linux乱码问题
  4. ros安装教程unbuntu20.04
  5. VUE3.0从安装到应用 (2)
  6. windows10 No module named ‘win32con‘ 亲测解决
  7. box-shadow:单边阴影与多边阴影
  8. 腾达F6路由器无线中继功能设置
  9. 洛谷 T284709 怨念(resent)
  10. java文件名 目录名或卷标语法不正确_大神求解,IO报错文件名、目录名或卷标语法不正确...