@@ -106,6 +106,82 @@ private Node rebuildCore(T[] pre_order, int pre_start, int pre_end, T[] mid_orde
106106 return null ;
107107 }
108108
109+ /**
110+ * 求一个树中两个节点的公共节点,这是第一中方法,下面还有第二中方法
111+ *
112+ * @param one
113+ * @param other
114+ * @return
115+ */
116+ public T commonParent (T one , T other ) {
117+ Preconditions .checkNotNull (one );
118+ Preconditions .checkNotNull (other );
119+ if (isEmpty ()) {
120+ return null ;
121+ } else {
122+ Node result = getCommonNode (getNodePath (one ), getNodePath (other ));
123+ return (result == null ) ? null : result .getValue ();
124+ }
125+ }
126+
127+ private T getCommonParent (Node start_node , T one , T other ) {
128+ Preconditions .checkNotNull (one );
129+ Preconditions .checkNotNull (other );
130+ if (start_node == null ) {
131+ return null ;
132+ }
133+
134+ T left_parent ;
135+ if ((left_parent = getCommonParent (start_node .getLeft (), one , other )) != null ) {
136+ return null ;
137+ }
138+
139+ T right_parent ;
140+ if ((right_parent = getCommonParent (start_node .getRight (), one , other )) != null ) {
141+ return null ;
142+ }
143+
144+ return null ;
145+ }
146+
147+ private Node getCommonNode (Stack <Node > one , Stack <Node > other ) {
148+ Preconditions .checkNotNull (one );
149+ Preconditions .checkNotNull (other );
150+ if (one .isEmpty () || other .isEmpty ()) {
151+ return null ;
152+ }
153+ Node last_common_node = null ;
154+ Node last_node ;
155+ while (!one .isEmpty () && !other .isEmpty () && (last_node = one .pop ()).equals (other .pop ())) {
156+ last_common_node = last_node ;
157+ }
158+ return last_common_node ;
159+ }
160+
161+
162+ private Stack <Node > getNodePath (T ele ) {
163+ Preconditions .checkNotNull (ele );
164+ Stack <Node > stack = new LinkedStack <>();
165+ if (!isEmpty ()) {
166+ getNodePathCore (_head , ele , stack );
167+ }
168+ return stack ;
169+ }
170+
171+ private boolean getNodePathCore (Node start , T ele , Stack <Node > path_stack ) {
172+ Preconditions .checkNotNull (ele );
173+ Preconditions .checkNotNull (path_stack );
174+ if (start == null ) {
175+ return false ;
176+ }
177+ if (start .getValue ().equals (ele ) || getNodePathCore (start .getLeft (), ele , path_stack ) || getNodePathCore (start .getRight (), ele , path_stack )) {
178+ path_stack .push (start );
179+ return true ;
180+ }
181+
182+ return false ;
183+ }
184+
109185
110186 private Vector <LinkedList <Node >> createLevelLinkedList () {
111187 Vector <LinkedList <Node >> result = new Vector <>();
@@ -515,15 +591,15 @@ public Node(Node left, Node parent, Node right) {
515591
516592 @ Override
517593 public String toString () {
518- final StringBuilder sb = new StringBuilder ("\t Node {" );
594+ final StringBuilder sb = new StringBuilder ("Node {" );
519595 sb .append ("height=" ).append (height );
520596 sb .append (", value=" ).append (value );
521- if (left != null ) {
522- sb .append (",\n \t left=" ).append (left );
523- }
524- if (right != null ) {
525- sb .append (",\n \t right=" ).append (right );
526- }
597+ // if (left != null) {
598+ // sb.append(",\n\t left=").append(left);
599+ // }
600+ // if (right != null) {
601+ // sb.append(",\n\t right=").append(right);
602+ // }
527603 sb .append ('}' );
528604 return sb .toString ();
529605 }
0 commit comments