FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

049_week02_homework by bugcodes · Pull Request #414 · algorithm001/algorithm · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (2) .md  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
51 changes: 51 additions & 0 deletions Week_02/id_49/LeetCode_609_49.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.v0ex.leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Find Duplicate File in System
*
* @author bugcoder
*/
public class LeetCode_609_49 {

/**
* 用哈希表来处理,key就是文件的内容,value就是文件的路径
* @param paths
* @return
*/
public List<List<String>> findDuplicate(String[] paths) {
Map<String,List<String>> map = new HashMap<>();
for (String path : paths) {
String[] split = path.split(" ");
String rootPath = split[0]+"/";
for (int i = 1; i < split.length; i++) {
String file = split[i];
String fileName = file.substring(0,file.indexOf("("));
//拆分字符串获取文件的内容
String fileContent = file.substring(file.indexOf("(")+1, file.lastIndexOf(")"));
//获取文件的全部路径
String filePath = rootPath + fileName;
if (map.containsKey(fileContent)){
List<String> list = map.get(fileContent);
list.add(filePath);
map.put(fileContent,list);
}else {
List<String> list = new ArrayList<>();
list.add(filePath);
map.put(fileContent,list);
}
}
}
List<List<String>> result = new ArrayList<>();
for (Map.Entry<String,List<String>> entry : map.entrySet()){
if (entry.getValue().size()>1){
result.add(entry.getValue());
}
}
return result;
}
}
44 changes: 44 additions & 0 deletions Week_02/id_49/LeetCode_671_49.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.v0ex.leetcode;

import java.util.HashSet;
import java.util.Set;

/**
* Second Minimum Node In a Binary Tree
*
* @author bugcoder
*/
public class LeetCode_671_49 {

public int findSecondMinimumValue(TreeNode root) {
Set<Integer> set = new HashSet<Integer>();
inOrder(root,set);
if(set.size() < 2){
return -1;
}
//根据题意,第一个值肯定是最小的值
int first = root.val;
//先临时设置第二小的值是整数的最大值,后续会对这个值进行替换
int second = Integer.MAX_VALUE;
for(int val : set){
if(first < val && val <= second){
second = val;
}
}
return second <= Integer.MAX_VALUE ? second : -1;
}

/**
* 中序遍历二叉树,把所有的值存放在Set集合中
* @param root
* @param set
*/
private void inOrder(TreeNode root,Set<Integer> set){
if(root == null){
return;
}
inOrder(root.left,set);
set.add(root.val);
inOrder(root.right,set);
}
}
47 changes: 46 additions & 1 deletion Week_02/id_49/NOTE.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
# 学习笔记
# 学习笔记
之前对于二叉树的前中后遍历,一直停留在学生时代,给定一个二叉树,能够从ABCD中选出正确答案,但是要写出递归代码,总是把自己给递进去归不来,那种深深的挫败感一直停留在对算法的黑洞里。第二周的作业,在求解LeetCode671. Second Minimum Node In a Binary Tree时,忽然想到了王争老师第三章的课件中对于二叉树遍历的讲解,四五行代码,那种感觉,简单优雅,豁然开朗。于我个人而言,这是我这周最大的收获!

二叉树的中遍历

void inOrder(TreeNode root){
if(null == root){
return;
}
//先-左节点
inOrder(root.left);
//然后-根节点,得到当前节点的val,进行相关的逻辑处理
root.val;
//最后-右节点
inOrder(root.right);
}

二叉树的前序遍历

void preOrder(TreeNode root){
if(null == root){
return;
}
//先-根节点,得到当前节点的val,进行相关的逻辑处理
root.val;
//然后-左节点
inOrder(root.left);
//最后-右节点
inOrder(root.right);
}

二叉树的后序遍历

void lastOrder(TreeNode root){
if(null == root){
return;
}
//先-左节点
inOrder(root.left);
//然后-右节点
inOrder(root.right);
//最后-根节点,得到当前节点的val,进行相关的逻辑处理
root.val;
}

二叉树的前中后序遍历可以把二叉树表示为数组,那如果知道某个二叉树的前序数组和中序数据,如何还原二叉树?

Back | FazBrowse Home | New Git URL