226. 翻转二叉树

题目来源:226. 翻转二叉树

翻转一棵二叉树。

示例:

输入:

     4
   /     2     7
 / \   / 1   3 6   9

输出:

     4
   /     7     2
 / \   / 9   6 3   1
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
var invertTree = function(root) {
  if(!root){
    return null
  }
  let left = invertTree(root.left)
  let right = invertTree(root.right)
  root.left = right
  root.right = left
  return root
};

Python3

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def invertTree(self, root: TreeNode) -> TreeNode:
      if not root:
        return root
      left  = self.invertTree(root.left)
      right = self.invertTree(root.right)

      root.left, root.right = right, left
      return root      

 



226. 翻转二叉树

上一篇:stat命令和lstat函数


下一篇:通过Nginx反向代理解决Jeecg-boot前端跨域访问的问题。