镜像二叉树和求二叉树最大深度(java)

时间:2025-02-12 07:49:15
* Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public TreeNode invertTree(TreeNode root) { if(root == null){ return null; } process(root); return root; } public TreeNode process(TreeNode root){ if(root == null){ return null; } TreeNode left = process(root.left); TreeNode right = process(root.right); root.left = right; root.right = left; return root; } }

相关文章