Leetcode226.翻轉二叉樹 Invert Binary Tree(Java)
Leetcode226.翻轉二叉樹 Invert Binary Tree(Java)
##Tree##
翻轉一顆二叉樹,採用前序遍歷
每次遞迴交換左右子樹
空結點時返回空結點或不處理空結點
本題也可以採用其他遍歷方式,例如迭代法,層次遍歷等
時間複雜度: O(n)
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root != null) {
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
invertTree(root.left);
invertTree(root.right);
}
return root;
}
}
相關文章
- LeetCode Invert Binary TreeLeetCode
- LeetCode 156 Binary Tree Upside Down 上下翻轉二叉樹LeetCodeIDE二叉樹
- 二叉搜尋樹(Binary Search Tree)(Java實現)Java
- Maximum Depth of Binary Tree 二叉樹的深度二叉樹
- [CareerCup] 4.1 Balanced Binary Tree 平衡二叉樹二叉樹
- Leetcode 226. Invert Binary TreeLeetCode
- [LeetCode] 226. Invert Binary TreeLeetCode
- 如何在Java中實現二叉搜尋樹( binary search tree)?Java
- 226. Invert Binary Tree--LeetCode RecordLeetCode
- 257. Binary Tree Paths(列印二叉樹所有路徑)二叉樹
- LeetCode 545. Boundary of Binary Tree 二叉樹邊界LeetCode二叉樹
- 二叉搜尋樹(Binary Search Tree )的定義及分析
- Binary Tree Traversals(HDU1710)二叉樹的簡單應用二叉樹
- 【LeetCode 144_二叉樹_遍歷】Binary Tree Preorder TraversalLeetCode二叉樹
- 【LeetCode 110_二叉樹_遍歷】Balanced Binary TreeLeetCode二叉樹
- 【LeetCode 104_二叉樹_遍歷】Maximum Depth of Binary TreeLeetCode二叉樹
- JavaScript 二叉搜尋樹以及實現翻轉二叉樹JavaScript二叉樹
- 二叉樹翻轉(分治思想的典型)二叉樹
- 【LeetCode 111_二叉樹_遍歷】Minimum Depth of Binary TreeLeetCode二叉樹
- Binary Tree Inorder/Preorder Traversal 返回中序和前序/遍歷二叉樹的元素集合二叉樹
- [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉樹的最小共同父節點二叉樹
- [CareerCup] 4.4 Create List at Each Depth of Binary Tree 二叉樹的各層建立連結串列二叉樹
- [LeetCode] 1367. Linked List in Binary Tree 二叉樹中的連結串列LeetCode二叉樹
- 力扣 653. 兩數之和 IV 二叉樹/binary-tree two-sum IV力扣二叉樹
- Construct Binary Tree from Preorder and Inorder Traversal(前序遍歷和中序遍歷樹構造二叉樹)...Struct二叉樹
- Balanced Binary Tree leetcode javaLeetCodeJava
- Amazing tree —— 二叉查詢樹
- LeetCode 297. Serialize and Deserialize Binary Tree 二叉樹序列化反序列化LeetCode二叉樹
- JS中的演算法與資料結構——二叉查詢樹(Binary Sort Tree)JS演算法資料結構
- 【LeetCode 235_二叉搜尋樹】Lowest Common Ancestor of a Binary Search TreeLeetCode
- Recover Binary Search Tree leetcode javaLeetCodeJava
- Binary Tree Inorder Traversal leetcode javaLeetCodeJava
- Binary Tree Preorder Traversal leetcode javaLeetCodeJava
- Binary Tree Postorder Traversal leetcode javaLeetCodeJava
- Maximum Depth of Binary Tree leetcode javaLeetCodeJava
- POJ2255Tree Recovery(二叉樹)二叉樹
- 【C++】翻轉二叉樹(遞迴、非遞迴)C++二叉樹遞迴
- Java集合原始碼分析之基礎(五):平衡二叉樹(AVL Tree)Java原始碼二叉樹