pull/6/head
yuanguangxin 5 years ago
parent acadcd8944
commit 104f1ff5a1

@ -1,26 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="af7ffdf2-4ddc-4ed6-8222-60ed5acbc2ed" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/src/etc/tree/traversal/Main.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/etc/tree/traversal/TreeNode.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/q144/Solution.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/q144/TreeNode.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/q145/Solution.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/q145/TreeNode.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/q94/Solution.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/q94/TreeNode.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/q98/Solution.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/q98/TreeNode.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/q98/f2/Solution.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/q98/f2/TreeNode.java" afterDir="false" />
<list default="true" id="af7ffdf2-4ddc-4ed6-8222-60ed5acbc2ed" name="Default Changelist" comment="add q94/98/144/145">
<change afterPath="$PROJECT_DIR$/src/q98/f3/Solution.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/q98/f3/TreeNode.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/etc/ali/q4/Info.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/etc/ali/q4/Info1.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/etc/ali/q4/Info2.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/etc/ali/q4/Info3.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/etc/ali/q4/Main.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/q20/Solution.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/q20/Solution.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/q98/Solution.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/q98/f1/Solution.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/q98/TreeNode.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/q98/f1/TreeNode.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/q98/f2/Solution.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/q98/f2/Solution.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -83,11 +70,11 @@
</component>
<component name="RecentsManager">
<key name="CopyClassDialog.RECENTS_KEY">
<recent name="q98.f3" />
<recent name="q98.f2" />
<recent name="q145" />
<recent name="q94" />
<recent name="q144" />
<recent name="etc.tree.traversal" />
<recent name="q98.f2" />
</key>
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/src/etc/q1" />
@ -95,7 +82,7 @@
<recent name="$PROJECT_DIR$/src/q206/f1" />
</key>
</component>
<component name="RunManager" selected="Application.Main">
<component name="RunManager" selected="Application.Solution (1)">
<configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="etc.tree.traversal.Main" />
<module name="LeetCode" />
@ -110,11 +97,11 @@
</method>
</configuration>
<configuration name="Solution (1)" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="q98.Solution" />
<option name="MAIN_CLASS_NAME" value="q98.f3.Solution" />
<module name="LeetCode" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="q98.*" />
<option name="PATTERN" value="q98.f3.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
@ -161,6 +148,19 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Solution" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="q98.f1.Solution" />
<module name="LeetCode" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="q98.f1.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Solution" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="q7.f1.Solution" />
<module name="LeetCode" />
@ -189,11 +189,11 @@
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.Solution (1)" />
<item itemvalue="Application.Main" />
<item itemvalue="Application.Solution (4)" />
<item itemvalue="Application.Solution (1)" />
<item itemvalue="Application.Solution (3)" />
<item itemvalue="Application.Solution (2)" />
</list>
</recent_temporary>
</component>
@ -234,7 +234,7 @@
<workItem from="1580044287086" duration="50376000" />
<workItem from="1580562331089" duration="8289000" />
<workItem from="1580626297484" duration="35811000" />
<workItem from="1581061282899" duration="13542000" />
<workItem from="1581061282899" duration="19690000" />
</task>
<task id="LOCAL-00001" summary="add q5/q7/q172">
<created>1580045439607</created>
@ -313,7 +313,14 @@
<option name="project" value="LOCAL" />
<updated>1580965798400</updated>
</task>
<option name="localTasksCounter" value="12" />
<task id="LOCAL-00012" summary="add q94/98/144/145">
<created>1581102125245</created>
<option name="number" value="00012" />
<option name="presentableId" value="LOCAL-00012" />
<option name="project" value="LOCAL" />
<updated>1581102125245</updated>
</task>
<option name="localTasksCounter" value="13" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -331,7 +338,8 @@
<MESSAGE value="add q53" />
<MESSAGE value="add q54" />
<MESSAGE value="add q23/q101" />
<option name="LAST_COMMIT_MESSAGE" value="add q23/q101" />
<MESSAGE value="add q94/98/144/145" />
<option name="LAST_COMMIT_MESSAGE" value="add q94/98/144/145" />
</component>
<component name="WindowStateProjectService">
<state x="320" y="115" key="#Inspections" timestamp="1581061018990">
@ -346,66 +354,66 @@
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="533" y="166" key="#com.intellij.ide.util.MemberChooser/0.0.1440.900@0.0.1440.900" timestamp="1578548165321" />
<state x="303" y="50" key="CommitChangelistDialog2" timestamp="1581102087738">
<state x="303" y="50" key="CommitChangelistDialog2" timestamp="1581156768511">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="303" y="50" key="CommitChangelistDialog2/0.0.1440.900@0.0.1440.900" timestamp="1581102087738" />
<state x="144" y="78" key="DiffContextDialog" timestamp="1580965769981">
<state x="303" y="50" key="CommitChangelistDialog2/0.0.1440.900@0.0.1440.900" timestamp="1581156768511" />
<state x="144" y="78" key="DiffContextDialog" timestamp="1581102103344">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="144" y="78" key="DiffContextDialog/0.0.1440.900@0.0.1440.900" timestamp="1580965769981" />
<state width="1398" height="181" key="GridCell.Tab.0.bottom" timestamp="1581101006950">
<state x="144" y="78" key="DiffContextDialog/0.0.1440.900@0.0.1440.900" timestamp="1581102103344" />
<state width="1398" height="181" key="GridCell.Tab.0.bottom" timestamp="1581156216396">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="181" key="GridCell.Tab.0.bottom/0.0.1440.900@0.0.1440.900" timestamp="1581101006950" />
<state width="1398" height="280" key="GridCell.Tab.0.bottom/0.23.1440.797@0.23.1440.797" timestamp="1581052570065" />
<state width="1398" height="181" key="GridCell.Tab.0.center" timestamp="1581101006947">
<state width="1398" height="181" key="GridCell.Tab.0.bottom/0.0.1440.900@0.0.1440.900" timestamp="1581156216396" />
<state width="1398" height="181" key="GridCell.Tab.0.bottom/0.23.1440.797@0.23.1440.797" timestamp="1581153638155" />
<state width="1398" height="181" key="GridCell.Tab.0.center" timestamp="1581156216394">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="181" key="GridCell.Tab.0.center/0.0.1440.900@0.0.1440.900" timestamp="1581101006947" />
<state width="1398" height="280" key="GridCell.Tab.0.center/0.23.1440.797@0.23.1440.797" timestamp="1581052570062" />
<state width="1398" height="181" key="GridCell.Tab.0.left" timestamp="1581101006946">
<state width="1398" height="181" key="GridCell.Tab.0.center/0.0.1440.900@0.0.1440.900" timestamp="1581156216394" />
<state width="1398" height="181" key="GridCell.Tab.0.center/0.23.1440.797@0.23.1440.797" timestamp="1581153638148" />
<state width="1398" height="181" key="GridCell.Tab.0.left" timestamp="1581156216393">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="181" key="GridCell.Tab.0.left/0.0.1440.900@0.0.1440.900" timestamp="1581101006946" />
<state width="1398" height="280" key="GridCell.Tab.0.left/0.23.1440.797@0.23.1440.797" timestamp="1581052570061" />
<state width="1398" height="181" key="GridCell.Tab.0.right" timestamp="1581101006949">
<state width="1398" height="181" key="GridCell.Tab.0.left/0.0.1440.900@0.0.1440.900" timestamp="1581156216393" />
<state width="1398" height="181" key="GridCell.Tab.0.left/0.23.1440.797@0.23.1440.797" timestamp="1581153638147" />
<state width="1398" height="181" key="GridCell.Tab.0.right" timestamp="1581156216395">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="181" key="GridCell.Tab.0.right/0.0.1440.900@0.0.1440.900" timestamp="1581101006949" />
<state width="1398" height="280" key="GridCell.Tab.0.right/0.23.1440.797@0.23.1440.797" timestamp="1581052570063" />
<state width="1398" height="261" key="GridCell.Tab.1.bottom" timestamp="1581061279043">
<state width="1398" height="181" key="GridCell.Tab.0.right/0.0.1440.900@0.0.1440.900" timestamp="1581156216395" />
<state width="1398" height="181" key="GridCell.Tab.0.right/0.23.1440.797@0.23.1440.797" timestamp="1581153638149" />
<state width="1398" height="261" key="GridCell.Tab.1.bottom" timestamp="1581156036596">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="261" key="GridCell.Tab.1.bottom/0.0.1440.900@0.0.1440.900" timestamp="1581061279043" />
<state width="1398" height="261" key="GridCell.Tab.1.bottom/0.0.1440.900@0.0.1440.900" timestamp="1581156036596" />
<state width="1398" height="304" key="GridCell.Tab.1.bottom/0.23.1440.797@0.23.1440.797" timestamp="1580408030266" />
<state width="1398" height="261" key="GridCell.Tab.1.center" timestamp="1581061279042">
<state width="1398" height="261" key="GridCell.Tab.1.center" timestamp="1581156036595">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="261" key="GridCell.Tab.1.center/0.0.1440.900@0.0.1440.900" timestamp="1581061279042" />
<state width="1398" height="261" key="GridCell.Tab.1.center/0.0.1440.900@0.0.1440.900" timestamp="1581156036595" />
<state width="1398" height="304" key="GridCell.Tab.1.center/0.23.1440.797@0.23.1440.797" timestamp="1580408030265" />
<state width="1398" height="261" key="GridCell.Tab.1.left" timestamp="1581061279042">
<state width="1398" height="261" key="GridCell.Tab.1.left" timestamp="1581156036594">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="261" key="GridCell.Tab.1.left/0.0.1440.900@0.0.1440.900" timestamp="1581061279042" />
<state width="1398" height="261" key="GridCell.Tab.1.left/0.0.1440.900@0.0.1440.900" timestamp="1581156036594" />
<state width="1398" height="304" key="GridCell.Tab.1.left/0.23.1440.797@0.23.1440.797" timestamp="1580408030262" />
<state width="1398" height="261" key="GridCell.Tab.1.right" timestamp="1581061279043">
<state width="1398" height="261" key="GridCell.Tab.1.right" timestamp="1581156036596">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="261" key="GridCell.Tab.1.right/0.0.1440.900@0.0.1440.900" timestamp="1581061279043" />
<state width="1398" height="261" key="GridCell.Tab.1.right/0.0.1440.900@0.0.1440.900" timestamp="1581156036596" />
<state width="1398" height="304" key="GridCell.Tab.1.right/0.23.1440.797@0.23.1440.797" timestamp="1580408030266" />
<state x="221" y="63" key="SettingsEditor" timestamp="1581061096545">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="221" y="63" key="SettingsEditor/0.0.1440.900@0.0.1440.900" timestamp="1581061096545" />
<state x="320" y="190" key="Vcs.Push.Dialog.v2" timestamp="1580965799877">
<state x="320" y="190" key="Vcs.Push.Dialog.v2" timestamp="1581102126556">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="320" y="190" key="Vcs.Push.Dialog.v2/0.0.1440.900@0.0.1440.900" timestamp="1580965799877" />
<state x="378" y="207" width="683" height="486" key="find.popup" timestamp="1580830143783">
<state x="320" y="190" key="Vcs.Push.Dialog.v2/0.0.1440.900@0.0.1440.900" timestamp="1581102126556" />
<state x="378" y="207" width="683" height="486" key="find.popup" timestamp="1581148315369">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="378" y="207" width="683" height="486" key="find.popup/0.0.1440.900@0.0.1440.900" timestamp="1580830143783" />
<state x="378" y="207" width="683" height="486" key="find.popup/0.0.1440.900@0.0.1440.900" timestamp="1581148315369" />
<state x="385" y="196" width="670" height="676" key="search.everywhere.popup" timestamp="1580563351587">
<screen x="0" y="0" width="1440" height="900" />
</state>

@ -1,4 +1,4 @@
package q98;
package q98.f1;
import java.util.ArrayList;
import java.util.List;

@ -1,4 +1,4 @@
package q98;
package q98.f1;
public class TreeNode {
int val;

@ -1,32 +1,32 @@
package q98.f2;
import java.util.Stack;
/**
* o(n)
*/
public class Solution {
public boolean isValidBST(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
while (!stack.empty() || root != null) {
while (root != null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
System.out.println(root.val);
root = root.right;
public boolean valid(TreeNode root, Integer min, Integer max) {
if (root == null) {
return true;
}
int val = root.val;
if (min != null && val <= min) {
return false;
}
if (max != null && val >= max) {
return false;
}
if (!valid(root.left, min, val)) {
return false;
}
if (!valid(root.right, val, max)) {
return false;
}
return true;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(5);
TreeNode n1 = new TreeNode(1);
TreeNode n2 = new TreeNode(4);
root.left = n1;
root.right = n2;
TreeNode n3 = new TreeNode(3);
TreeNode n4 = new TreeNode(6);
n2.left = n3;
n2.right = n4;
System.out.println(new Solution().isValidBST(root));
public boolean isValidBST(TreeNode root) {
return valid(root, null, null);
}
}

@ -0,0 +1,58 @@
package q98.f3;
import java.util.LinkedList;
import java.util.Queue;
/**
* o(n)
*/
public class Solution {
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
Queue<Integer> min = new LinkedList<>();
Queue<Integer> max = new LinkedList<>();
while (!queue.isEmpty()) {
TreeNode temp = queue.poll();
Integer maxt = max.poll();
Integer mint = min.poll();
if (mint != null && temp.val <= mint) {
return false;
}
if (maxt != null && temp.val >= maxt) {
return false;
}
if (temp.left != null) {
min.add(mint);
max.add(temp.val);
queue.add(temp.left);
}
if (temp.right != null) {
max.add(maxt);
min.add(temp.val);
queue.add(temp.right);
}
}
return true;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(10);
TreeNode n1 = new TreeNode(5);
TreeNode n2 = new TreeNode(15);
root.left = n1;
root.right = n2;
TreeNode n3 = new TreeNode(6);
TreeNode n4 = new TreeNode(20);
n2.left = n3;
n2.right = n4;
System.out.println(new Solution().isValidBST(root));
}
}

@ -0,0 +1,11 @@
package q98.f3;
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
Loading…
Cancel
Save