二叉树反转

invert a binary tree.

     4
   /     2     7
 / \   / 1   3 6   9

to

     4
   /     7     2
 / \   / 9   6 3   1

递归代码1:
TreeNode* invertTree(TreeNode* root) {
	if(root==NULL)
			return ;
		if ( root->left != NULL) invertTree(root->left);
                if ( root->right != NULL) invertTree(root->right);
		TreeNode * ptmpNode = root->left;
                root->left = root-right ;
                root->right = ptmpNode ;
    }

  

 递归代码2:

   TreeNode* invertTree(TreeNode* root) {
	if(root==NULL)
			return NULL;
		TreeNode * ptmpNode = root->left;
		root->left = invertTree(root->right);
		root->right = invertTree(ptmpNode);
		return root;
    }

  

时间: 2024-08-04 04:51:19

二叉树反转的相关文章

Leetcode 226: Invert Binary Tree(二叉树反转)

nvert 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 engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a wh

LeetCode226:Invert Binary Tree 二叉树反转

题目 226. Invert Binary Tree QuestionEditorial Solution My Submissions Total Accepted: 109341 Total Submissions: 230799 Difficulty: Easy Invert a binary tree.      4    /     2     7  / \   / 1   3 6   9 to      4    /     7     2  / \   / 9   6 3   1

一道算法题目, 二行代码, Binary Tree

June 8, 2015 我最喜欢的一道算法题目, 二行代码. 编程序需要很强的逻辑思维, 多问几个为什么, 可不可以简化.想一想, 二行代码, 五分钟就可以搞定; 2015年网上大家热议的 Homebrew 的作者 Max Howell 面试 Google 挂掉的一题, 二叉树反转, 七行代码, 相比二行代码, 情有可原! Problem: return the count of binary tree with only one child 想一想, 你要写几行, 六七行, 或小于十行? S

一文教你学会递归解题

前言 递归是算法中一种非常重要的思想,应用也很广,小到阶乘,再在工作中用到的比如统计文件夹大小,大到 Google 的 PageRank 算法都能看到,也是面试官很喜欢的考点 最近看了不少递归的文章,收获不小,不过我发现大部分网上的讲递归的文章都不太全面,主要的问题在于解题后大部分都没有给出相应的时间/空间复杂度,而时间/空间复杂度是算法的重要考量!递归算法的时间复杂度普遍比较难(需要用到归纳法等),换句话说,如果能解决递归的算法复杂度,其他算法题题的时间复杂度也基本不在话下.另外,递归算法的时

反转二叉树

输入一个二叉树,输出其镜像. 如下图,即交换所有节点的左右子树. 这里提供两种思路:使用递归和不使用递归. 使用的二叉树定义如下: public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } 解决方法: import java.util.LinkedList; import java.util.Sca

LeetCode 226. Invert Binary Tree (反转二叉树)

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 engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a wh

【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

反转二叉树,即交换所有结点的左右子树,但不能使用递归方法。

反转二叉树,即交换所有结点的左右子树,但不能使用递归方法. 解析:既然不能使用递归那么可以使用栈,代码如下: #include <iostream> #include<stack> #include<assert.h> #include <tchar.h> #include <queue> using namespace std; typedef struct BinaryTreeNode { int m_nValue; BinaryTreeNo

C/C++ - 反转二叉树

由上而下递归反转代码: #include <stdio.h> #include <stdlib.h> typedef struct Tree { int val; struct Tree* left; struct Tree* right; }Tree; void CreateBiTree(Tree**T) { int val; scanf("%d", &val); if(val == -1) *T = NULL; else { *T = (Tree *