这不,前不久就用一道“invert a binary tree"干掉了homebrew的初创者Max Howell。 随着Max在twitter上吐槽(原推),这个”invert a binary tree"分分钟就火了。 Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off. L...
#include<iostream>#include<vector>#include<algorithm>using namespacestd;structnode{intid, l, r, index, level; } a[100];vector<node> v1;voiddfs(introot,intindex,intlevel){if(a[root].r !=-1) dfs(a[root].r, index *2+2, level +1); v1.push_back({root,0,0, index, level});...
step0 step1这道题关于 倒置树/镜像树的思路其实在1043 Is It a Binary Search Tree,关键就是通过不建树,改变子节点遍历顺序的方法来输出倒置顺序。另外就是多了个查找根节点的环节,这个只要从头到尾逐个找就好…
A1102 Invert a Binary Tree [反转二叉树/镜像] 给一棵二叉树子节点下标,要求反转二叉树,之后按要求输出层序和中序遍历。 思路:因为是给的子节点下标,用二叉树的静态写法会更加方便,找出根节点后,按照后序遍历反转二叉树(交换左右子树),然后再按照模板层序中序遍历输出 #include<iostream>#include<vector>#include...
PAT.A1102 Invert a Binary Tree 题意 给出一颗编号节点为0-n-1的二叉树,给出每个节点的孩子结点编号,输出把该二叉树反转后的层次遍历和中序遍历序列。 样例(可复制) 8 1- -- 0- 27 -- -- 5- 46 1. 2. 3. 4. 5. 6. 7. 8....
226. Invert Binary Tree 题目 Invert a binary tree. Example: Input: 4/\27/\/\1369 1. 2. 3. 4. 5. Output: 4/\72/\/\9631 1. 2. 3. 4. 5. Trivia: This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (...
Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 我以为很难,原来还是递归 网友说,递归不难,是一种思想。我认同,原来认为递归难,是因为还不具备用递归解决问题的习惯和思维方式。一旦把问题交给递归解决,发现竟然很省心。对,就是省心二字。
Invert a binary tree. **注:返回tree的镜像树 思路1 用recursive的方法,先判断if (root) 然后交换其左右两个node的位置 并在将该方法延伸到其左右两个node 思路2 不用recursive的方法,用一个stack存还没有完成swap的node 每次pop出来一个node,并swap其左右两个node,并分别push到stack中 ...
Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off. Now it's your turn to prove that YOU CAN invert a binary tree! 输入格式 Each input file contains one test case. For each case, the first line gives...
Can you solve this real interview question? Invert Binary Tree - Given the root of a binary tree, invert the tree, and return its root. Example 1: [https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg] Input: root = [4,2,7,1,3,6,9] Outp