175. Invert Binary Tree【LintCode by java】

Description

Invert a binary tree.

Example

  1         1
 / \       / 2   3  => 3   2
   /         4         4

 

解题:题目要求讲二叉树的左子树和右子树对调一下,用递归来做很简单:

 1 /**
 2  * Definition of TreeNode:
 3  * public class TreeNode {
 4  *     public int val;
 5  *     public TreeNode left, right;
 6  *     public TreeNode(int val) {
 7  *         this.val = val;
 8  *         this.left = this.right = null;
 9  *     }
10  * }
11  */
12
13 public class Solution {
14     /**
15      * @param root: a TreeNode, the root of the binary tree
16      * @return: nothing
17      */
18     public void invertBinaryTree(TreeNode root) {
19         // write your code here
20         if(root == null)
21             return ;
22         TreeNode left = root.left;
23         TreeNode right = root.right;
24         root.left = right;
25         root.right = left;
26         invertBinaryTree(root.left);
27         invertBinaryTree(root.right);
28     }
29 }

非递归法:

 1 public class Solution {
 2     public TreeNode invertTree(TreeNode root) {
 3         Queue<TreeNode> q = new LinkedList<TreeNode>();
 4         if(root!=null) q.offer(root);
 5         while(!q.isEmpty()){
 6             TreeNode curr = q.poll();
 7             TreeNode tmp = curr.right;
 8             curr.right = curr.left;
 9             curr.left = tmp;
10             if(curr.left!=null) q.offer(curr.left);
11             if(curr.right!=null) q.offer(curr.right);
12         }
13         return root;
14     }
15 }

原文地址:https://www.cnblogs.com/phdeblog/p/9192678.html

时间: 2024-11-12 08:54:34

175. Invert Binary Tree【LintCode by java】的相关文章

156. Merge Intervals【LintCode by java】

Description Given a collection of intervals, merge all overlapping intervals. Example Given intervals => merged intervals: [ [ (1, 3), (1, 6), (2, 6), => (8, 10), (8, 10), (15, 18) (15, 18) ] ] Challenge O(n log n) time and O(1) extra space. 题意:给定一个

158. Valid Anagram【LintCode by java】

Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification What is Anagram? Two strings are anagram if they can be the same after change the order of characters. Example Given s = "abcd", t = "dcab&q

165. Merge Two Sorted Lists【LintCode by java】

Description Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list should be made by splicing together the nodes of the two lists and sorted in ascending order. Example Given 1->3->8->11->15->null,

212. Space Replacement【LintCode by java】

Description Write a method to replace all spaces in a string with %20. The string is given in a characters array, you can assume it has enough space for replacement and you are given the true length of the string. You code should also return the new

172. Remove Element【LintCode by java】

Description Given an array and a value, remove all occurrences of that value in place and return the new length. The order of elements can be changed, and the elements after the new length don't matter. Example Given an array [0,4,4,0,0,2,4,4], value

181. Flip Bits【LintCode, by java】

Description Determine the number of bits required to flip if you want to convert integer n to integer m. Both n and m are 32-bit integers. Example Given n = 31 (11111), m = 14 (01110), return 2. 解题:比较两个整数对应的二进制数,共有多少位不同.注意,负数也包含在内.">>>"

【LeetCode-面试算法经典-Java实现】【226-Invert Binary Tree(反转二叉树)】

[226-Invert Binary Tree(反转二叉树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 题目大意 将一棵二叉树进行翻转. 解题思路 对每一个结点,将它的左右子树进行交换,再对它的左右子结点进行同样的操作. 代码实现 树结点类 pub

【07_226】Invert Binary Tree

Invert Binary Tree Total Accepted: 54994 Total Submissions: 130742 Difficulty: Easy Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our enginee

[LintCode] Invert Binary Tree 翻转二叉树

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. Example Given 4 points: (1,2), (3,6), (0,0), (1,3). The maximum number is 3. LeeCode上的原题,可参见我之前的博客Invert Binary Tree 翻转二叉树. 解法一: // Recursion class So