[LeetCode Daily Question] 226. Flip Binary Tree
226. Flip Binary Tree
Topic source link
algorithm idea: recursion; tree;
The tree structure requires flipping as follows:
java code
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
invert(root);
return root;
}
public void invert(TreeNode root){
//图形要求,先序遍历二叉树,翻转
if(root == null){
return;
}
//先序遍历:根(操作),左,右
TreeNode temp = new TreeNode();
temp = root.left;
root.left = root.right;
root.right = temp;
invert(root.left);
invert(root.right);
}
}