Skip to content

Commit

Permalink
feat: add 2415
Browse files Browse the repository at this point in the history
  • Loading branch information
Joyee691 committed Dec 20, 2024
1 parent b6fea34 commit e3c860a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
- [2096. Step-By-Step Directions From a Binary Tree Node to Another(medium)](./book_sources/medium/2096.md)
- [2191. Sort the Jumbled Numbers(medium)](./book_sources/medium/2191.md)
- [2196. Create Binary Tree From Descriptions(medium)](./book_sources/medium/2196.md)
- [2415. Reverse Odd Levels of Binary Tree(medium)](./book_sources/medium/2415.md)
- [2418. Sort the People(easy)](./book_sources/easy/2418.md)
- [2751. Robot Collisions(hard)](./book_sources/hard/2751.md)

Expand Down
1 change: 1 addition & 0 deletions book_sources/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
- [2096. Step-By-Step Directions From a Binary Tree Node to Another(medium)](./medium/2096.md)
- [2191. Sort the Jumbled Numbers(medium)](./medium/2191.md)
- [2196. Create Binary Tree From Descriptions(medium)](./medium/2196.md)
- [2415. Reverse Odd Levels of Binary Tree(medium)](./medium/2415.md)
- [2418. Sort the People(easy)](./easy/2418.md)
- [2751. Robot Collisions(hard)](./hard/2751.md)

1 change: 1 addition & 0 deletions book_sources/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
- [2096. Step-By-Step Directions From a Binary Tree Node to Another(medium)](./medium/2096.md)
- [2191. Sort the Jumbled Numbers(medium)](./medium/2191.md)
- [2196. Create Binary Tree From Descriptions(medium)](./medium/2196.md)
- [2415. Reverse Odd Levels of Binary Tree(medium)](./medium/2415.md)
- [Hard](hard/README.md)
- [32. Longest Valid Parentheses(hard)](./hard/32.md)
- [51. N-Queens(hard)](./hard/51.md)
Expand Down
57 changes: 57 additions & 0 deletions book_sources/medium/2415.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 2415. Reverse Odd Levels of Binary Tree

> [Leetcode link](https://leetcode.com/problems/reverse-odd-levels-of-binary-tree)
## 题目简介

题目给我们一个完美二叉树(所有父节点都有两个子节点且所有叶子节点都在同一层),要求我们将奇数层的所有叶子节点翻转(简单理解成 Array.reverse() 就好

## 解题思路

其实翻转的本质可以看成,有两个指针分别指向需要交换的层的最左边与最右边的节点,然后将其交换后把指针各自往中间走一步,直到指针相遇

那么我们在树中要怎么样找到当前层最左跟最右的节点呢?答案是 dfs

我们只需要在深度遍历的时候,同时遍历两个节点就好了,所以 dfs 的调用应该是这样的:

`dfs(node.left, node.right, level)`

那么我们接下来要怎么让两个 “指针” 同时往中间走呢?答案是反过来遍历:

`dfs(node.right, node.left, level)`

基于以上,我们可以看代码了

### Javascript

```js
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var reverseOddLevels = function(root) {
const dfs = (left, right, level) => {
if(left === null || right === null) {
return
}
if(level % 2 === 1) {
[left.val, right.val] = [right.val, left.val]
}
dfs(left.left, right.right, level+1)
dfs(left.right, right.left, level+1)
}

dfs(root.left, root.right, 1)
return root

};
```

0 comments on commit e3c860a

Please sign in to comment.