LeetCode 226. Invert Binary Tree
題目
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 (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
翻譯
反轉一個二元樹。
這靈感來至於 Max Howell,
Google 有90%使用你的軟件, 但你無法反轉二元樹
想法
遞回方式, 左右互換
詳細參閱
ex :
1下面有子節點, 2跟3互換
6跟7, 4跟5互換
7, 6, 5, 4 後面沒子節點, return
Code
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function(root) {
//空的或是 left right 都沒存在就直接return
if ( !root || (!root.left && !root.right) ) {
return root;
}
//先把左邊記錄下來, 左右互換
let temp = root.left;
root.left = invertTree(root.right);
root.right = invertTree(temp);
return root;
};
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function (root) {
if ( !root ) {
return root;
}
[root.left, root.right] = [ invertTree(root.right), invertTree(root.left) ];
return root;
};
Run
留言
張貼留言