Leetcode题解之 —— 翻转二叉树
思路
先序遍历
先序遍历, 交换左右节点, 注意叶子节点、空节点、单个根节点特殊情况的处理
题解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
var invertTree = function(root) { if (!root) { return null; } if (!root.left && !root.right) { return root; }
[root.left, root.right] = [root.right, root.left]; invertTree(root.left); invertTree(root.right);
return root; };
|