226. Invert Binary Tree

来源:互联网 发布:中企动力域名管理 编辑:程序博客网 时间:2024/05/22 00:54

Invert a binary tree.

     4   /   \  2     7 / \   / \1   3 6   9
to
     4   /   \  7     2 / \   / \9   6 3   1

解题思路:

利用递归方式解决,左孩子为右递归,右孩子为左递归。


Java-Solution

public class Solution {public TreeNode invertTree(TreeNode root) {if(root == null)return null;TreeNode temp = root.left;root.left = invertTree(root.right);root.right = invertTree(temp);return root;    }}





0 0
原创粉丝点击