mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
@ -363,6 +363,54 @@ Python:
|
|||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
```go
|
||||||
|
/**
|
||||||
|
* Definition for a binary tree node.
|
||||||
|
* type TreeNode struct {
|
||||||
|
* Val int
|
||||||
|
* Left *TreeNode
|
||||||
|
* Right *TreeNode
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
// 递归
|
||||||
|
func defs(left *TreeNode, right *TreeNode) bool {
|
||||||
|
if left == nil && right == nil {
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
if left == nil || right == nil {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
if left.Val != right.Val {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return defs(left.Left, right.Right) && defs(right.Left, left.Right);
|
||||||
|
}
|
||||||
|
func isSymmetric(root *TreeNode) bool {
|
||||||
|
return defs(root.Left, root.Right);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 迭代
|
||||||
|
func isSymmetric(root *TreeNode) bool {
|
||||||
|
var queue []*TreeNode;
|
||||||
|
if root != nil {
|
||||||
|
queue = append(queue, root.Left, root.Right);
|
||||||
|
}
|
||||||
|
for len(queue) > 0 {
|
||||||
|
left := queue[0];
|
||||||
|
right := queue[1];
|
||||||
|
queue = queue[2:];
|
||||||
|
if left == nil && right == nil {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if left == nil || right == nil || left.Val != right.Val {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
queue = append(queue, left.Left, right.Right, right.Left, left.Right);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
JavaScript
|
JavaScript
|
||||||
```javascript
|
```javascript
|
||||||
|
@ -284,6 +284,55 @@ Python:
|
|||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
```go
|
||||||
|
/**
|
||||||
|
* Definition for a binary tree node.
|
||||||
|
* type TreeNode struct {
|
||||||
|
* Val int
|
||||||
|
* Left *TreeNode
|
||||||
|
* Right *TreeNode
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
func max (a, b int) int {
|
||||||
|
if a > b {
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
// 递归
|
||||||
|
func maxDepth(root *TreeNode) int {
|
||||||
|
if root == nil {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return max(maxDepth(root.Left), maxDepth(root.Right)) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 遍历
|
||||||
|
func maxDepth(root *TreeNode) int {
|
||||||
|
levl := 0;
|
||||||
|
queue := make([]*TreeNode, 0);
|
||||||
|
if root != nil {
|
||||||
|
queue = append(queue, root);
|
||||||
|
}
|
||||||
|
for l := len(queue); l > 0; {
|
||||||
|
for ;l > 0;l-- {
|
||||||
|
node := queue[0];
|
||||||
|
if node.Left != nil {
|
||||||
|
queue = append(queue, node.Left);
|
||||||
|
}
|
||||||
|
if node.Right != nil {
|
||||||
|
queue = append(queue, node.Right);
|
||||||
|
}
|
||||||
|
queue = queue[1:];
|
||||||
|
}
|
||||||
|
levl++;
|
||||||
|
l = len(queue);
|
||||||
|
}
|
||||||
|
return levl;
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
JavaScript
|
JavaScript
|
||||||
```javascript
|
```javascript
|
||||||
|
@ -301,6 +301,64 @@ class Solution:
|
|||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
```go
|
||||||
|
/**
|
||||||
|
* Definition for a binary tree node.
|
||||||
|
* type TreeNode struct {
|
||||||
|
* Val int
|
||||||
|
* Left *TreeNode
|
||||||
|
* Right *TreeNode
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
func min(a, b int) int {
|
||||||
|
if a < b {
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
// 递归
|
||||||
|
func minDepth(root *TreeNode) int {
|
||||||
|
if root == nil {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if root.Left == nil && root.Right != nil {
|
||||||
|
return 1 + minDepth(root.Right);
|
||||||
|
}
|
||||||
|
if root.Right == nil && root.Left != nil {
|
||||||
|
return 1 + minDepth(root.Left);
|
||||||
|
}
|
||||||
|
return min(minDepth(root.Left), minDepth(root.Right)) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 迭代
|
||||||
|
|
||||||
|
func minDepth(root *TreeNode) int {
|
||||||
|
dep := 0;
|
||||||
|
queue := make([]*TreeNode, 0);
|
||||||
|
if root != nil {
|
||||||
|
queue = append(queue, root);
|
||||||
|
}
|
||||||
|
for l := len(queue); l > 0; {
|
||||||
|
dep++;
|
||||||
|
for ; l > 0; l-- {
|
||||||
|
node := queue[0];
|
||||||
|
if node.Left == nil && node.Right == nil {
|
||||||
|
return dep;
|
||||||
|
}
|
||||||
|
if node.Left != nil {
|
||||||
|
queue = append(queue, node.Left);
|
||||||
|
}
|
||||||
|
if node.Right != nil {
|
||||||
|
queue = append(queue, node.Right);
|
||||||
|
}
|
||||||
|
queue = queue[1:];
|
||||||
|
}
|
||||||
|
l = len(queue);
|
||||||
|
}
|
||||||
|
return dep;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
JavaScript:
|
JavaScript:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user