[LeetCode226]Invert Binary Tree】的更多相关文章

题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 反转二叉树,左右儿子值交换 代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), ri…
翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1  思路 如果根节点存在,就交换两个子树的根节点,用递归,从下至上.. 解一: auto tmp = invertTree(root->left); 将左子树整体 当作一个节点 交换. 最后返回根节点. /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tr…
Invert Binary Tree Total Accepted: 54994 Total Submissions: 130742 Difficulty: Easy Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our…
1 题目 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 接口: public TreeNode invertTree(TreeNode root) 2 思路 反转一颗二叉树. 可以用递归和非递归两种方法来解. 递归的方法,写法非常简洁,五行代码搞定,交换当前左右节点,并直接调用递归即可. 非递归的方法,参考树的层序遍历,借助Queue来辅助,先把根节点排入队列中,然后从队中取出来,交换其左…
226. Invert Binary Tree Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 代码实现 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NU…
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = function(num) { var b = (num-1) % 9 + 1 ; return b; }; //之所以num要-1再+1;是因为特殊情况下:当num是9的倍数时,0+9的数字根和0的数字根不同. 性质说明 1.任何数加9的数字根还是它本身.(特殊情况num=0)        小学学加法的…
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - 226. Invert Binary Tree - 题解 在线提交: https://leetcode.com/problems/invert-binary-tree/ 或 http://www.nowcoder.com/practice/564f4c26aa584921bc75623e48ca301…
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. Example Given 4 points: (1,2), (3,6), (0,0), (1,3). The maximum number is 3. LeeCode上的原题,可参见我之前的博客Invert Binary Tree 翻转二叉树. 解法一: // Recursion class So…
LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homeb…
题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solution  Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell:…