226. 翻转二叉树-leetcode
226. 翻转二叉树-leetcode
1 题目
2 代码
/** * Definition for a binary tree node. * class TreeNode { * public $val = null; * public $left = null; * public $right = null; * function __construct($value) { $this->val = $value; } * } */ class Solution { /** * @param TreeNode $root * @return TreeNode */ function invertTree($root) { if (empty($root)) { return $root; } $tmp = $root->left; $root->left = $root->right; $root->right = $tmp; $this->invertTree($root->left); $this->invertTree($root->right); return $root; } }
简单题,但是可以不用递归解题 广度优先搜索的思想(Breadth-fist Search, BFS)也可以解题
本文来自博客园,作者:吴丹阳-V,转载请注明原文链接:https://www.cnblogs.com/wudanyang/p/13576965.html